• iOS 关于页面跳转和传值


    一、iOS 页面间的跳转目前有3种方式:

    1、利用StroyBorad

    这里以TableView的静态cell为例,选中第一个cell按住ctrl往新的ViewController上拖,弹出对话框选择show或present modally

    2、代码跳转

    - (IBAction)Push:(id)sender {

        CATransition *animation = [CATransition animation];

        [animation setDuration:0.3];

        [animation setType: kCATransitionMoveIn];

        [animation setSubtype: kCATransitionFromTop];

        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];

        UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        PresentViewController * info = [mainStoryboard instantiateViewControllerWithIdentifier:@"PresentViewController"];

        

        [self.navigationController pushViewController:info animated:NO];

        [self.navigationController.view.layer addAnimation:animation forKey:@"1"];

    }

    这是navigationController的push 有默认的返回

    - (IBAction)presentView:(id)sender {

        UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        PresentViewController * info = [mainStoryboard instantiateViewControllerWithIdentifier:@"PresentViewController"];

        

        self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

        [self presentViewController:info animated:YES completion:^{

        }];

    }

    这是弹出模态框。调用

        [self dismissViewControllerAnimated:YES completion:^{

        }];返回上个界面

     

    3、Seque跳转

    这个方式要设定Seque的identifier进行动态绑定加载。选定源控制器的Controller(红色圆圈)

    按住ctrl拖到要显示的Controller上,在弹出框上选择弹出类型。在属性中修改这个Seque的identifier=PushCtrl

    代码:

    [self performSegueWithIdentifier:@"PushCtrl" sender:self]; 这样就会跳转到这个identifier指定的页面上。

    关于show,present modally可以参考官方文档 https://developer.apple.com/library/ios/recipes/xcode_help-IB_storyboard/chapters/StoryboardSegue.html

    二、页面间的数据传递

    一般通过 segue和delegate实现两个页面传值,有A,B两个页面 通过还可以利用页面跳转的unwind实现数据回传

    1、传入数据

    A、利用segue

    在A页面中加入prepareForSegue函数,A显示B页面之前会调用该函数,可利用该函数传值给B

    segue.destinationViewController表示目标页面这里是B

    segue.sourceViewController表示源页面这里是A

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
     UIViewController *viewController=segue.destinationViewController;
     SecondViewController *secondViewController=(SecondViewController *)viewController;
     secondViewController.defaultTitleValue=self.titleLabel.text;
     secondViewController.defaultPriceValue=self.priceLabel.text;

    }

    B、在初始化B页面的时候直接传入数据

     UIStoryboard * mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

     UIViewController * info = [mainStoryboard instantiateViewControllerWithIdentifier:@"SecondViewController"];

    2、返回数据

    A、delegate

    声明一个protocol协议

    @protocol PassValueDelegate <NSObject>

     -(void)setValue:(NSDictionary *)dictionary;

    @end

    @interface PassValueDelegate : NSObject

    @end

    在B中声明一个Delegate属性,它是继承了BackValueDelegate的对象

    id<PassValueDelegate> delegate;

    把想要传递给A的数据放入到setValue中 [delegate setVaule:数据];

    在A中实现 -(void)setValue:(NSDictionary *)dictionary;

    这样就完成了数据的回传

    B、unwindsegue

    关于unwindsegue参考 http://www.cnblogs.com/ZJUT-jiangnan/p/3902638.html

    可以实现A-B、 B-C 跳转,还可以直接从C-A的跳转

    1、在A中实现

    - (IBAction)unwindSegueToRedViewController:(UIStoryboardSegue *)segue {

        

    }

    2、将左边的Controller按住ctrl键往EXIT上拖出现此对话框,选中unwindSegueToRedViewController

    3、选中exit的这个方法,拖拽至YellowViewController的back redvc按钮。放手之后,会在back redvc附近出现一个action的提示,选中。这样,当点击back red vc按钮之后,就会从C跳回A。

  • 相关阅读:
    Unity 自定义日志保存
    一万字详解 Redis Cluster Gossip 协议
    第八届“图灵杯”NEUQ-ACM程序设计竞赛个人赛非官方题解
    数组小游戏---火把萤石
    11个编程接单的网站,你有技术就有收入,有收入就有女朋友《男盆友》
    逆向工程,调试Hello World !程序(更新中)
    魔改一波合成大西瓜!代码已开源~
    如何使用C++做个简单推箱子游戏
    第八届“图灵杯”NEUQ-ACM程序设计竞赛个人赛非官方题解
    zookeeper应用
  • 原文地址:https://www.cnblogs.com/HypeCheng/p/4226500.html
Copyright © 2020-2023  润新知