Uzair Dhada
Uzair Dhada

Reputation: 333

How to show viewcontroller from the view?

I searched for answers like Get to UIViewController from UIView? and couple of other answers but was not successful.

My issue is that I have a button in UIView lets say class1 and when I click on that button I want to load another view class2 which is UIViewController, and as I don't get navigationController in class1 I am unable to load the class2 view.

Please help me with this.

Thanks, In Advance.

Upvotes: 0

Views: 245

Answers (4)

ajay_nasa
ajay_nasa

Reputation: 2298

First fill storyboard ID with "MyViewController", which is a String field that you can use to create a new ViewController based on that storyboard ViewController. And later access that view controller like this:

- (IBAction)buttonPressed:(id)sender{
    MyCustomViewController *newvc = [self.storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
   [self presentViewController:newvc animated:YES completion:nil];
}

Upvotes: 1

knutigro
knutigro

Reputation: 1092

In general UIViews should not contain any logic that triggers the flow of app. This is the job of UIViewControllers. It's just a way of making the design of your code better and more organized.

One way I often use is to use a delegate pattern in my custom UIViews. Here is s simple setup:

In your MyCustomView .h file:

@class MyCustomView;

@protocol MyCustomViewDelegate <NSObject>
@optional
- (void)myViewDidTapOnButton:(MyCustomView)myCustomView;
@end


@interface MyCustomView : UIView

@property (weak, nonatomic) id <MyCustomViewDelegate> delegate;

@end

In your MyCustomView .m file:

- (IBAction)didTapMyButton:(id)sender {

    if ([self.delegate respondsToSelector:@selector(myViewDidTapOnButton:)]) {
        [self.delegate myViewDidTapOnButton:self];
    }
}

Then in your viewcontroller, which is presenting your view:

interface:

@interface MyViewController ()<MyCustomViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *myCustomView;

and implementation:

- (void)viewDidLoad {
    [super viewDidLoad];

    self.myCustomView.delegate = self;
}

- (void)myViewDidTapOnButton:(MyCustomView)myCustomView {
    ... code for presenting viewcontroller ...
}

Note: Even if you dont use the parameter myCustomView which is sent in this method, its a common pattern and good habit to always send the sender of the delegate as the first parameter.

This is also used a lot by Apple, e.g. in

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;

Upvotes: 1

Vinay Jain
Vinay Jain

Reputation: 1661

Two cases :

  • If you are using storyboard then give your NavigationController a storyboard id. And create an object of navigationController in your custom UIView class.

  • If you have customized the app launching from AppDelegate create a public property of your navigationController. From your UIView class create an object of appDelegate with [UIApplication sharedApplication].delegate. From this object access the navigationController property

When you have the navigationController object you can push your viewcontroller with:

[navigationController pushViewController:ViewController animated:YES];

Upvotes: 1

Scott
Scott

Reputation: 162

When you click your button,you can do this:

YouViewController *yourViewController = [YouViewController new];
[self.view addSubView:yourViewController.view];

Hope to help you.

Upvotes: 0

Related Questions