Reputation: 11338
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
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
Reputation: 29767
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<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.
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
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:
paying particular attention to the Protocols and Delegation sections.
Also
Upvotes: 0
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