0

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.

Community
  • 1
  • 1
Uzair Dhada
  • 333
  • 2
  • 6
  • 23

4 Answers4

1

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;
knutigro
  • 1,082
  • 2
  • 10
  • 20
1

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

Vinay Jain
  • 1,653
  • 20
  • 28
  • Yes i am having `AppDelegate` and i also have property of `navigationController` but when i do `self.navigationController` i don't get the navigation controller. so I have to call `pushViewController` with the object of `appdelegate`? – Uzair Dhada Jul 23 '15 at 12:24
  • no, viewcontrollers can be pushed only on `navigationController` objects – Vinay Jain Jul 23 '15 at 12:27
  • I tried the same but its not working, code gets executed but nothing happens. – Uzair Dhada Jul 23 '15 at 12:30
1

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];
}
ajay_nasa
  • 2,278
  • 2
  • 28
  • 45
  • Thanks, ajay for your effort :-) but i have not used storyboards, and i got the solution, which was given by knutigro. – Uzair Dhada Jul 27 '15 at 06:06
0

When you click your button,you can do this:

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

Hope to help you.

Scott
  • 162
  • 1
  • 12