一个不小心犯的指针错误

尝试达到下面的效果:

输入框输入字符,单击按钮改变label框的值:

ViewController.h

 1 #import <UIKit/UIKit.h>
 2 
 3 @interface ViewController : UIViewController
 4 
 5 @property (nonatomic,assign) IBOutlet UITextField *input1;
 6 @property (nonatomic,assign) IBOutlet UILabel *output1;
 7 
 8 
 9 - (IBAction)click1;
10 @end

 

ViewController.m

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 
 5 @end
 6 
 7 @implementation ViewController
 8 
 9 - (void)viewDidLoad {
10     [super viewDidLoad];
11     // Do any additional setup after loading the view, typically from a nib.
12 }
13 
14 - (void)didReceiveMemoryWarning {
15     [super didReceiveMemoryWarning];
16     // Dispose of any resources that can be recreated.
17 }
18 - (void)click1{
19 //    NSString *input1 = _input1.text;
20 //    NSString *output1 = _output1.text;
21 //    output1 = input1;
22     NSString *input1 = _input1.text;
23     _output1.text = input1;
24     NSString *output1 = _output1.text;
25     
26     
27     NSLog(@"test:%@,%@",input1,output1);
28     
29     
30 }
31 
32 @end

click1方法里面我一开始是写的注释掉的3行,运行后正常打印,但是label的text值并未改变。原因是"output1 = input1"只是改变了output1指向而已,使output1这个指向了input1所指向的地址,而_output1的set方法并不会被调用。好吧,我也不知道为什么开始会那样写orz.

 

posted @ 2015-03-07 15:55  mikado_Q  阅读(80)  评论(0编辑  收藏  举报