Devang
Devang

Reputation: 11338

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

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?

Upvotes: 0

Views: 1617

Answers (4)

D.C.
D.C.

Reputation: 15588

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

Upvotes: 1

beryllium
beryllium

Reputation: 29767

  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
    }
    

Upvotes: 1

Ashley Mills
Ashley Mills

Reputation: 53171

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

Upvotes: 0

PawanShakya
PawanShakya

Reputation: 97

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

Upvotes: 0

Related Questions