温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

属性传值和协议传值  

发布时间:2020-07-13 12:41:26 来源:网络 阅读:525 作者:Im刘亚芳 栏目:开发技术

属性传值三部.....

1.在第二个页面.h中,定义name

//属性传值............1

@property (nonatomic, copy)NSString *name;

2.在第一页.m中然后在推出第二个页面前,把第一个按钮的title值传给第二个页面定义的name

SecondViewController *secondVC = [[SecondViewController alloc] init];

    //属性传值..............2

    secondVC.name = button.currentTitle//把按钮名给第二个页面的name

    [self.navigationController pushViewController:secondVC animated:YES];

    [secondVC release];

3.在第二页.m中让TextField来接收第一页传过来的值

//属性传值........3

    self.TextField.text = self.name;   //接收从第一页传过来的name


协议传值六部走........

协议传值六个步骤分别是在第二个页面三部,在第一个页面三个;

1.首先在第二页的.h中自定义一个协议SecondPassValueDelegate,然后在里面写一个方法,用来改变按钮的title

//协议传值----1

//指定一个协议

@protocol SecondPassValueDelegate <NSObject>


- (void)changeButtonTitle:(NSString *)title;


@end


2.在第二页.h中,定义一个代理人对象属性(记得为id类型,并且是assign)

//协议传值----2

//定一个代理人对象属性

//只有实现了上面定义的协议方法的对象,才能为第二个页面的代理人

@property (nonatomic , assign)id <SecondPassValueDelegate>delegate;


3.在第二页.m方法中 ,让自己的代理人去执行约定好的方法,获得里面的值

/协议传值----3

    //让自己的代理人(delegate)去执行约定好的方法  获取里面的值

    [self.delegate changeButtonTitle:_TextField.text];

4.在第一个页面签订第二个页面的协议 ,对应第一步的创建协议    (这里的协议可以在.h中写,也可以在.m中私有方法实现.---这里是后者 .)

//协议传值-----4

//第一个页面签订第二个页面的协议   对应第一步的创建协议


@interface FirstViewController () <SecondPassValueDelegate>


@end

5.在第一个页面的.m方法中 ,把自己指定为第二个页面的代理人

//把自己指定为第二个页面的代理人

    secondVC.delegate = self;

6.在第一个页面中实现协议的方法

- (void)changeButtonTitle:(NSString *)title

{

    //获得按钮

    UIButton *button = (UIButton *)[self.view viewWithTag:1000];

    //给按钮 重新设置标题

    [button setTitle:title forState:UIControlStateNormal];

    

    UIButton *button1 = (UIButton *)[self.view viewWithTag:2000];

    [button1 setTitle:title forState:UIControlStateNormal];

}


 

类和文件

AppDelegate.m

#import "AppDelegate.h"
#import "FirstViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    FirstViewController *firstVC = [[FirstViewController alloc] init];
    UINavigationController *naviVC = [[UINavigationController alloc] initWithRootViewController:firstVC];
    self.window.rootViewController = naviVC;
    [firstVC release];
    
    [_window release];
    return YES;
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end


FirstViewController.h

#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@end

FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"
//协议传值-----4
//第一个页面签订第二个页面的协议   对应第一步的创建协议
@interface FirstViewController () <SecondPassValueDelegate>
@end
@implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"第一页";
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(120, 100, 80, 25)];
    [button setTitle:@"按钮" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.layer.cornerRadius = 5;
    button.tag = 1000;
    button.backgroundColor = [UIColor cyanColor];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    [button release];
    
    
    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(120, 160, 80, 25)];
    [button1 setTitle:@"asdsadf" forState:UIControlStateNormal];
    [button1 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    button1.layer.cornerRadius = 5;
    button1.tag = 2000;
    button1.backgroundColor = [UIColor blueColor];
    [button1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    [button1 release];
    
}
- (void)buttonClicked:(UIButton *)button
{
    SecondViewController *secondVC = [[SecondViewController alloc] init];
    //属性传值..............2
    secondVC.name = button.currentTitle;  //把按钮名给第二个页面的name
    //协议传值------5
    //把自己指定为第二个页面的代理人
    secondVC.delegate = self;
    
    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];
}
//协议传值-------6
//实现协议方法
- (void)changeButtonTitle:(NSString *)title
{
    //获得按钮
    UIButton *button = (UIButton *)[self.view viewWithTag:1000];
    //给按钮 重新设置标题
    [button setTitle:title forState:UIControlStateNormal];
    
    UIButton *button1 = (UIButton *)[self.view viewWithTag:2000];
    [button1 setTitle:title forState:UIControlStateNormal];
}
- (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
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end



SecondViewController.h

#import <UIKit/UIKit.h>
//协议传值----1;
//指定一个协议
@protocol SecondPassValueDelegate <NSObject>
- (void)changeButtonTitle:(NSString *)title;
@end
@interface SecondViewController : UIViewController
@property (nonatomic , retain)UITextField *TextField;
//属性传值............1
@property (nonatomic, copy)NSString *name;
//协议传值----2
//定一个代理人对象属性
//只有实现了上面定义的协议方法的对象,才能为第二个页面的代理人
@property (nonatomic , assign)id <SecondPassValueDelegate>delegate;
@end

SecondViewController.m

#import "SecondViewController.h"
@interface SecondViewController () <UITextFieldDelegate>
@end
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.title = @"第二页";
    self.view.backgroundColor = [UIColor brownColor];
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(120, 150, 100, 35)];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    button.layer.cornerRadius = 5;
    button.backgroundColor = [UIColor magentaColor];
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
    self.TextField = [[UITextField alloc] initWithFrame:CGRectMake(80, 80, 160, 50)];
    self.TextField.backgroundColor = [UIColor whiteColor];
    self.TextField.delegate = self;
    self.TextField.clearButtonMode = UITextFieldViewModeAlways;
    //属性传值........3
    self.TextField.text = self.name;   //接收从第一页传过来的name
    self.TextField.borderStyle =UITextBorderStyleRoundedRect;   //TextField的边框圆形的边框
    [self.view addSubview:self.TextField];
    [_TextField release];
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
    
}
- (void)buttonClicked:(UIButton *)button
{
    //协议传值----3
    //让自己的代理人(delegate)去执行约定好的方法  获取里面的值
    [self.delegate changeButtonTitle:_TextField.text];
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (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
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI