UINavigationController+storyboard

新建ios工程,然后打开Main.storyboard,点击Editor--Embed in--Navigation Controller


2、指定storyboard中ViewController 对应ViewController ,并为其添加按钮,以及点击事件


为按钮添加点击事件后的ViewController实现类

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)nextClick:(id)sender {
    
    // 点击按钮后开启新的UIViewController,新的UIViewController根据storyboard2这个标志来获取
    UIViewController* nextVc=[self.storyboard instantiateViewControllerWithIdentifier:@"storyboard2"];
    [self.navigationController pushViewController:nextVc animated:YES];
}

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

@end

3、新建ViewController2文件,并在storyboard中添加新的ViewController,然后指定id为storyboard2,关联ViewController2,并添加按钮点击事件


ViewCOntroller2实现类

#import "ViewController2.h"

@interface ViewController2 ()

@end

@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (IBAction)backClick:(id)sender {
    //返回上一级
    [self.navigationController popViewControllerAnimated:YES];
}

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

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

运行效果

          

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS 应用开发中,通常使用 UINavigationController 来管理多个页面的转换。在使用 UINavigationController 的情况下,你可以通过调用它的 pushViewController(_:animated:) 方法来转换到另一个页面。 举个例子,假设你有一个名为 FirstViewController 的视图控制器,并且要通过按钮点击事件来转换到另一个名为 SecondViewController 的视图控制器。 首先,需要在你的故事板中创建两个场景。并将场景关联到对应的视图控制器。 其次,在 FirstViewController 中添加一个按钮,并在它的点击事件中,调用 UINavigationController 的 pushViewController(_:animated:) 方法,将 SecondViewController 压入导航栈中 ``` swift import UIKit class FirstViewController: UIViewController { @IBOutlet weak var transitionButton: UIButton! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } @IBAction func transitionButtonDidTapped(_ sender: Any) { let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController self.navigationController?.pushViewController(secondViewController, animated: true) } } ``` 这样你就可以通过按钮点击来在两个场景间转换了。 需要注意的是,需要在你的 Appdelegate 里面配置 Navigation 并且设置第一个页面是哪一个. ```swift let firstViewController = self.storyboard?.instantiateViewController(withIdentifier: "FirstViewController") as! FirstViewController let navigationController = UINavigationController(rootViewController: firstViewController) self.window?.rootViewController = navigationController
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值