How can I switch views programmatically in a view

2019-01-10 08:02发布

Have been struggling with this for a while, and can never seem to get a direct answer.

Any help is appreciated!

11条回答
乱世女痞
2楼-- · 2019-01-10 08:35

I think the OP is asking how to swap a VIEW without changing viewCONTROLLERs. Am I misunderstanding his question?

In pseudocode, he wants to do:

let myController = instantiate(someParentController)

let view1 = Bundle.main.loadNib(....) as... blah myController.setThisViewTo( view1 )

let view2 = Bundle.main.loadNib(....) as... blah myController.setThisViewTo( view2 )

Am I getting his question wrong?

查看更多
何必那么认真
3楼-- · 2019-01-10 08:47

This worked for me:

NSTimer *switchTo = [NSTimer scheduledTimerWithTimeInterval:0.1
           target:selfselector:@selector(switchToTimer)userInfo:nil repeats:NO];

- (void) switchToTimer {
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil];
UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"MyViewControllerID"]; // Storyboard ID
[self presentViewController:vc animated:FALSE completion:nil];
}
查看更多
霸刀☆藐视天下
4楼-- · 2019-01-10 08:48

Swift 3.0 in

one view controller to secondviewcontroller go:

let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MsgViewController") as! MsgViewController
        self.navigationController?.pushViewController(loginVC, animated: true)

2nd viewcontroller to 1stviewcontroller(back)for: back button on action event:-

self.navigationController?.popViewController(animated:true)

3rdviewcontroller to 1st viewcontroller jump for

self.navigationController?.popToRootViewController(animated:true)

and most important storyboard in navigation bar unclicked make sure than this back action perform

查看更多
你好瞎i
5楼-- · 2019-01-10 08:49

To dismiss the Viewcontroller called with the previous answers code by CmdSft

    ViewController *viewController = [[ViewController alloc] init];    
    [self presentViewController:viewController animated:YES completion:nil];

you can use

    [self dismissViewControllerAnimated:YES completion: nil];
查看更多
smile是对你的礼貌
6楼-- · 2019-01-10 08:50

The instantiateViewControllerWithIdentifier is the Storyboard ID.

NextViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:@"NextViewController"];
[self presentViewController:NVC animated:YES completion:nil];
查看更多
▲ chillily
7楼-- · 2019-01-10 08:52

If you want to present a new view in the same storyboard,

In CurrentViewController.m,

#import "YourViewController.h"

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
YourViewController *viewController = (YourViewController *)[storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerIdentifier"];
[self presentViewController:viewController animated:YES completion:nil];

To set identifier to a view controller, Open MainStoryBoard.storyboard. Select YourViewController View-> Utilities -> ShowIdentityInspector. There you can specify the identifier.

查看更多
登录 后发表回答