IPhone : How to push or present UIViewController from UIView file?

Up vote 0 down vote favorite share g+ share fb share tw.

I want to push UIViewController from UIView. I have searched for that and I got link1, link2 but still I don't getting what changes I need to do.! My Code is as following KalGridView. H @interface KalGridView : UIView { } KalGridView.

M - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { // From here I want to push viewController. } How can I do This? Iphone objective-c cocoa-touch iphone-sdk-4.0 link|improve this question asked Oct 13 '11 at 9:57Devang1,887528 81% accept rate.

Usually you can access to UINavigationController via appDelegate. So if you have the UINavigationController property in your appDelegate try this code: (iMyApp_AppDelegate*)UIApplication sharedApplication delegate navigationController pushViewController:myVC animated:YES; Implement a delegate in your custom view: @protocol KalGridViewDelegate @interface KalGridView : UIView { id kgDelegate; } @property(nonatomic, assign) id kgDelegate; @protocol KalGridViewDelegate @optional -(void)didTouchInKalGridView:(KalGridView*)view withData:(NSObject*)data; @end KalGridView. M - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { self.

KgDelegate didTouchInKalGridView:self withData:someData; } So now you can handle this event in any place where is your custom view. Use NSNotification: - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSNotificationCenter defaultCenter postNotificationName:@"TOUCHED" object:nil; } In any point of your code: NSNotificationCenter defaultCenter addObserver:self selector:@selector(didTouched:) name:@"TOUCHED" object:nil; And method for handle (likely in other viewcontroller) - (void)didTouched:(NSNotification*)sender{ // push or pop your viewcontroller here }.

I am adding library source so I cannot import my delegate in its file. If I do so it is giving me Lexical file not found error – Devang Oct 13 '11 at 10:16 This is a terrible answer. In fact, any answer that includes (iMyApp_AppDelegate*)UIApplication sharedApplication delegate is a terrible answer.

You access a view controller's navigation controller using the viewController. NavigationController property. – Ashley Mills Oct 13 '11 at 10:23 There are no any viewControllers at all.

– beryllium Oct 13 '11 at 10:28 @Devang, check it now. – beryllium Oct 13 '11 at 10:57.

This is kind of a weird way to structure your design , but it is possible. Your view needs a delegate which is typically its owning view controller. When you create your view , set this delegate to the view controller.

Then in your touchedsmethod you have the reference you need to push a new controller -- update -- per request, here is what you might do: @interface KalGridView : UIView { } @property (nonatomic, assign) id delegate; @end @implementation KalGridView @synthesize delegate = _delegate; - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { // Use your delegate here to push a new view controller self. Delegate pushYourController; } @end @implementation KalGridViewController // ... wherever it is you make your KalGridView (OR in awakeFromNib) KalGridView *v = KalGridView new; v. Delegate = self; - (void)pushYourController { // this is the delegate callback where you really push your controller.

} @end.

– Devang Oct 13 '11 at 10:29 Ok, some code added. I should point out that @Ashley Mills solution is very similar so between both of these you should be set. – darren Oct 13 '11 at 21:43.

Take object of ViewController in which view is allocated. Push navigation controller with that instance to which ever view controller you want.

So, your view is either the main view of a viewController, or is contained somewhere in the view hierarchy of a viewController. I would create a delegate protocol, KalGridViewDelegate, with a method such as: - (void) kalGridViewWasTapped: (KalGridView *) kalGridView; Then make UIViewController who's view contains the KalGridView the delegate of that view. So within, say the viewDidLoad method of the view controller: self.kalGridView.

Delegate = self; Then within your touch method: - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { self. Delegate kalGridViewWasTapped: self; } and then your view controller can respond to the delegate method and push the next view controller as required. If you don't understand delegation, you should carefully read and understand the Apple docs: http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html paying particular attention to the Protocols and Delegation sections.

Also http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/CommunicateWithObjects.html#//apple_ref/doc/uid/TP40002974-CH7-SW18.

But still I am unable to see push or present methods after writing self.? – Devang Oct 13 '11 at 10:27 You do the push in the viewController that is the delegate of the KalGridView, NOT in the KalGridView itself. The KalGridView is tapped, sends a delegate message to the viewController, and the viewController displays the next view controller – Ashley Mills Oct 13 '11 at 10:30 I think I am very much poor with the delegate concept. If you can explain in more details.

– Devang Oct 13 '11 at 10:34 Delegation is a key design pattern used in Cocoa. I've added doc links to my answer – Ashley Mills Oct 13 '11 at 10:51.

Just make object of parent navigationcontroller and write objParent presentModalViewController:obj animated:YES.

View should not know anything about controller – beryllium Oct 13 '11 at 10:20 I think you are not coder that's why asking like this, I have implement this kind of thing in many application, I think you need to be first experienced person before giving negative rating....... – Neel Oct 13 '11 at 10:49 One more thing you need to know how to make parent of view..... – Neel Oct 13 '11 at 10:51 1 FYI, I don't give you down vote. Be more respectful. And make code better.

– beryllium Oct 13 '11 at 10:54 I think you need to just understand every statement carefully... – Neel Oct 13 '11 at 10:56.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions