How to push or present UIViewcontroller or storybo

2019-09-23 06:56发布

问题:

What I did:

  1. I am using scrollview to view some UIView(s), with paging enabled.
  2. In last page in subview of scrollView I added a button.
  3. I also wrote a function which is in subclass of UIView ,which is invoked when we press that button.
  4. I want to view storyboard when I press that button.

回答1:

You need to implement protocol method for custom UIView class.

For example;

#import <UIKit/UIKit.h>

@protocol YourCustomViewDelegate <NSObject>

- (void)buttonTapped;

@end

@interface YourCustomView : UIView

@property (nonatomic, strong) id< YourCustomViewDelegate > delegate;

@end

And the .m file you can call buttonTapped delegate method.

- (void)myCustomClassButtonTapped:(id)sender
{
    [self.delegate buttonTapped];
}

For proper work;

  • set your custom view delegate to self in your custom view controller.
  • add buttonTapped method to that view controller
  • And do what you want in that method. Present/Push view controllers like the other answers explained.

Edit

I will try to illustrate very simple usage of protocols, hope it will help you.

#import "MyViewController.h"
#import "YourCustomView.h"

@interface MyViewController ()<YourCustomViewDelegate> // this part is important

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    YourCustomView *customView = // initialize your custom view, I suppose that you already did it
    customView.delegate = self;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)buttonTapped
{
    YOUR_VIEW_CONTROLLER_CLASS *viewCon =[storyboard instantiateViewControllerWithIdentifier:@"mainVC"];    //mainVC is just a example and u will have to replace it with your viewController storyboard id

    //Pushing VC
    [self.navigationController pushViewController:viewCon animated:YES];
}


回答2:

Take a look at this

    YourViewController *obj=[[YourViewController alloc]init];
    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:obj];
    [[[UIApplication sharedApplication]keyWindow].rootViewController presentViewController:nav animated:YES completion:NULL];


回答3:

try like this

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController* initialView = [storyboard instantiateInitialViewController];


回答4:

Firstly you need to set a storyboard Id to your ViewController, namely "mainVC". Then you could pus/present it using below code :-

 YOUR_VIEW_CONTROLLER_CLASS *viewCon =[storyboard instantiateViewControllerWithIdentifier:@"mainVC"];    //mainVC is just a example and u will have to replace it with your viewController storyboard id

//Pushing VC
[self.navigationController pushViewController:viewCon animated:YES];
//OR presenting VC
[self presentViewController:viewCon animated:YES completion:nil];

Also in case if you want your another storyboard to be initiated then below,

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];   //Change MainStoryboard with your storyboard id


回答5:

hope this will help

the way i perceive your question is ->Rightnow your inside you are in UIView class and inside it there is a button by clicking it you want to push or present a ViewController and offcourse from inside your UIView class you cant push or present your view Controller. So

you can do one simple thing first inside action method make a ref(forward declaration) or you can make a whole new object of your ViewController class in which your UIView is present right now , then call a function(which exist in your ViewController) like

//function inside your uiview calss

- (void)btnaction
{
     [_objCSInformationViewController pushViewController];
} 

// function inside your view controller

-(void)pushViewController
{
yourViewController *objViewController = [[yourViewController alloc]initWithNibName:@"yourViewController" bundle:nil]; 
 [strong.navigationController pushViewController:objViewController animated:YES];

}