0

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?

Community
  • 1
  • 1
Devang
  • 11,258
  • 13
  • 62
  • 100

4 Answers4

1
  1. 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];

  2. Implement a delegate in your custom view:

    @protocol KalGridViewDelegate 
    @interface KalGridView : UIView  {
        id<KalGridViewDelegate> kgDelegate; }
    @property(nonatomic, assign) id<KalGridViewDelegate>    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.

  3. 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
    }
    
beryllium
  • 29,669
  • 15
  • 106
  • 125
  • 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
1

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
D.C.
  • 15,340
  • 19
  • 71
  • 102
0

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

0

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

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • 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