zoukankan      html  css  js  c++  java
  • iOS基础控件UINavigationController中的传值

    iOS基础控件UINavigationController中的传值,代理传值,正向传值,反向传值

    #import <UIKit/UIKit.h>
    
    //声明一个协议
    
    @protocol SendValue<NSObject>
    
    //定义一个方法
    
    - (void)sendBtnTitle:(NSString *)title;
    
    @end
    
    @interface FirstViewController : UIViewController
    
    // 定义代理
    
    @property (nonatomic, assign) id <SendValue>delegate;
    
    // 创建一个正向传值的属性
    
    @property (nonatomic,copy) NSString *currentTitle;
    
    @end
    - (void)didButton{
        NSArray *btntitles = [NSArray arrayWithObjects:@"按键一",@"按键二",@"按键三", nil];
        
        for (int i=0; i<btntitles.count; i++) {
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
            //改变按键字体颜色
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [btn setTitle:[btntitles objectAtIndex:i] forState:UIControlStateNormal];
    // 如果按钮的标题和属性中的 _currentTitle 相同,即和根页面中的导航条的 title 一样 
    if([_currentTitle isEqualToString:btn.currentTitle]){
    //开启选中状态 btn.selected
    = YES; btn.tintColor = [UIColor whiteColor]; } [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected]; [btn addTarget:self action:@selector(titlebtnClicked:) forControlEvents:UIControlEventTouchUpInside]; btn.frame = CGRectMake(80, 140+60*i, self.view.frame.size.width-160, 40); btn.backgroundColor = [UIColor whiteColor]; [self.view addSubview:btn]; } } - (void)titlebtnClicked:(UIButton *)btn{ NSString *title = btn.currentTitle;

        // 判断代理中是否有 sendBtnTitle :这个函数

        if ([_delegate respondsToSelector:@selector(sendBtnTitle:)]){

            // 代理 执行自己的 sendBtnTitle 函数,传参是 title

           [_delegate sendBtnTitle:title];

        }

        NSLog(@"%@",title);
        [self.navigationController popViewControllerAnimated:YES];
        
    }
    #import <UIKit/UIKit.h>
    #define C(a,b,c,d) [UIColor colorWithRed:a/255.0 green:b/255.0 blue:c/255.0 alpha:d/255.0]
    #define IMG(name) [UIImage imageNamed:name]
    
    
    #import "FirstViewController.h"
    //将协议挂过来
    @interface rootViewController : UIViewController<SendValue>
    
    @end
    - (void)btnClicked{
        FirstViewController *first = [[FirstViewController alloc] init];
        //将当前页面的navigationItem.title传递进去
        //正向传值
        first.currentTitle = self.navigationItem.title;
        //将代理指定为当前rootViewController类的指针
        first.delegate = self;
        [self.navigationController pushViewController:first animated:YES];
    }
    
    //实现协议定义的方法
    - (void)sendBtnTitle:(NSString *)title{
        self.navigationItem.title = title;
    }

    页面传值:

    正向传值利用是属性传值;

    反向传值利用代理传值;


    属性传值:如果从A页面往B页面传值,在B页面中声明属性,在A页面中跳转事件中给B页面的属性赋值;
    从后一个页面返回前一个页面不会执行前面页面的loadView和viewDidLoad方法,而是执行viewWillAppear方法,因为,loadView和viewDidLoad方法的作用是将视图加载到内存,而从后一个页面返回时,前一个页面已经加载到内存,所以不用再次加载,所以不执行loadView和ViewDidLoad方法;

    代理传值:和代理设计模式一样,按照设置代理,遵循协议的那四步来就行,

         /*

         1,定义协议  (BaoMuProtocol)

         2,遵循协议的类  (Nurse)

         3.定义需要代理的类  (Mother)

         4.建立关系   (Mother中定义一个代理类型的成员变量)     

         */

  • 相关阅读:
    2020软件工程作业01
    2020软件工程—06—个人作业
    团队二次作业
    软件工程作业05
    软件工程作业00--问题清单
    软件工程作业04二期
    2020软件工程作业04
    oracle11安装过程中常出现的问题和解决办法
    2020软件工程作业03
    2020软件工程作业02
  • 原文地址:https://www.cnblogs.com/hecheng0314/p/4472824.html
Copyright © 2011-2022 走看看