[Swift][OC]代码实现导航
先看看效果,很简单的东西
swift版:
sb里建立这样的控制器
别忘了设置storyboard ID
分别设置三个button的tag为101,102,103
@IBAction func BtnClick(sender: UIButton) { switch sender.tag { case 101: let redVC = storyboard?.instantiateViewControllerWithIdentifier("redVC") //先通过storyboard ID取得对应的ViewController self.navigationController?.pushViewController(redVC!, animated: true) //通过push方法压栈 case 102: let orangeVC = storyboard?.instantiateViewControllerWithIdentifier("orangeVC") self.navigationController?.pushViewController(orangeVC!, animated: true) case 103: let blueVC = storyboard?.instantiateViewControllerWithIdentifier("blueVC") self.navigationController?.pushViewController(blueVC!, animated: true) default: break } }
OC版:
.h文件中定义一个UIViewController
@property(nonatomic,assign)UIViewController *con;
.m文件中
- (IBAction)clicked:(UIButton *)sender { switch (sender.tag) { case 101: _con = [self.storyboard instantiateViewControllerWithIdentifier:@"redVC"]; [self.navigationController pushViewController:_con animated:YES]; break; case 102: _con = [self.storyboard instantiateViewControllerWithIdentifier:@"blueVC"]; [self.navigationController pushViewController:_con animated:YES]; break; default: break; } }
思路都是一样的,其中一般每个ViewController都有对应的类,上面的通过storyboard id初始化一个VC的时候可以这样写