StoryBoard中segue的使用
点击button跳转到下一个页面, 条件是textField里有值时才能实现跳转功能, 否则不跳转, 这个功能的实现就需要用到segue
首先在storyboard中viewController中拖入一个imageView textField UIButton, 在ViewController中拉入一条segue选择modal到下一个页面, 关联方法到ViewController中
在segue中添加标识
输入内容不为空得时候, 进行页面间跳转
触发标识符为pushToNextVC的这个sugue
performSegueWithIdentifier: sender: 用来触发segue
触发segue sender 用来传值
- (IBAction)clickMe:(id)sender { if (self.textField.text.length != 0) { [self performSegueWithIdentifier:@"pushToNextVC" sender:nil]; } else { //输入框为空的话, 提示重新输入 [[[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"您的输入框不能为空" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles: nil] show]; } } segue即将调用的方法 一般传值都在这里进行处理 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { //判断调用哪根线 //根据标识符 if ([segue.identifier isEqualToString:@"pushToNextVC"]) { //destinationViewController 目标视图控制器 SecondViewController *secVC = segue.destinationViewController; secVC.text = self.textField.text; } }
SecondViewController..m #import "SecondViewController.h" #import "MyTableViewCell.h" @interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate> - (IBAction)back:(id)sender; @property (weak, nonatomic) IBOutlet UITableView *tableView; @end @implementation SecondViewController - (void)dealloc { //安全释放(safe-release) //先释放后置空 self.myLabel = nil; } - (void)setMyLabel:(UILabel *)myLabel { if (_myLabel != myLabel) { _myLabel = myLabel; } } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //给label的text赋值 self.myLabel.text = self.text; //注册Cell //cell 的创建方式决定注册 class 还是 nib //XIB创建Cell, 就注册nib //和UITableView的创建方式无关 [self.tableView registerNib: [UINib nibWithNibName:@"MyTableViewCell" bundle:[NSBundle mainBundle] ]forCellReuseIdentifier:@"cellID"]; } - (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 { // NSLog(@"%@", sender); if ([segue.identifier isEqualToString:@"modalToNextVC"]) { //类型声明 NSIndexPath *path = (NSIndexPath *)sender; NSLog(@"%@", path); } } #pragma mark-UITableViewDelegate - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //触发segue //sender 用来传值 [self performSegueWithIdentifier:@"modalToNextVC" sender:indexPath]; } #pragma mark-UITableViewDataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //使用 SB 创建UITableView/UICollectionView的时候, 如果需要用到Cell的话, 建议XIB创建, 不要往UITableView里面拖Cell MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellID" forIndexPath:indexPath]; return cell; } - (IBAction)back:(id)sender { [self dismissViewControllerAnimated:YES completion:nil]; } @end
在这里修改Cell内部的控件布局
- (void)layoutSubviews { [super layoutSubviews]; }
效果图
时间: 2024-10-01 22:57:40