展示 second view controller 的几种方式

1. 保证第一vc 有navigation controller 并有segue,也就两个vc之间有连线

 **modal方式

***第二个vc上需要有返回按钮,

返回代码为

- (IBAction)tappedCloseModal:(id)sender {
     [self dismissModalViewControllerAnimated: YES];
}


**push方式

***back按钮自动生成

*若vc点击按钮关闭vc,

代码如下;

- (IBAction)tappedCloseModal:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


2. 保证第一vc 有navigation controller,但两个vc之间无连线: 代码形式

**push方式

***第一个vc上有导航按钮

***第二个vc的有storyboardID

导航按钮代码

- (IBAction)tappedNavButton:(id)sender {
    SecondViewController *secondViewController =     
        [self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
    [self.navigationController pushViewController:secondViewController animated:YES];
}


**modal 方式

- (IBAction)tappedShowNewView:(id)sender {
    SecondViewController *secondViewController =
        [self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];
        [self presentViewController:secondViewController animated:YES completion:nil]
}



3. 代理方式

**首先给第二个vc添加protocol 和delegate

@class SecondViewController;
 
@protocol SecondViewControllerDelegate <NSObject>
- (void)doSomethingWithSecondViewController:(SecondViewController *)secondViewController;
@end
 
@interface SecondViewController : UIViewController
@property (nonatomic, weak) id <SecondViewControllerDelegate> delegate;
- (IBAction)tappedCloseModal:(id)sender;
 
@end

**使用代理delegate

@implementation SecondViewController
 
@synthesize delegate;
 
- (IBAction)tappedCloseModal:(id)sender {
    [delegate doSomethingWithSecondViewController:self];

//如果不写 @synthesize delegate; 就要用 self.delegate
}


**使用protocol

首先在第一个vc中加入跳转方法


#import <UIKit/UIKit.h>
#import "SecondViewController.h"
 
@interface ViewController : UIViewController <SecondViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *lblInfo;
@property (weak, nonatomic) IBOutlet UITextField *txtText;
- (IBAction)tappedClickMe:(id)sender;
- (IBAction)tappedNavButton:(id)sender;
- (IBAction)tappedShowNewView:(id)sender;
 
@end

***初始化显示第二个vc,设置代理(第一个vc中)

- (IBAction)tappedShowNewView:(id)sender {
    SecondViewController *secondViewController =
        [self.storyboard instantiateViewControllerWithIdentifier:@"secondViewController"];

//什么时候发送代理权
    secondViewController.delegate = self;
    [self presentViewController:secondViewController animated:YES];
}

***实现方法(第二个vc中)--就是第二个页面要做的关闭操作。

- (void)doSomethingWithSecondViewController:(SecondViewController *)secondViewController {
    [self dismissModalViewControllerAnimated:YES];
}

如果第一个有navigation 

[self.navigationController popViewControllerAnimated:YES];






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值