Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS中单击UIButton时传递字符串值_Ios_Objective C_Cocoa Touch_Uibutton - Fatal编程技术网

在iOS中单击UIButton时传递字符串值

在iOS中单击UIButton时传递字符串值,ios,objective-c,cocoa-touch,uibutton,Ios,Objective C,Cocoa Touch,Uibutton,我已经检查了许多文章,查看在ios中单击uibutton时传递值的情况。 但一般来说,人们使用标记属性作为替代 但我找到了以下解决方案: UIButton *btnComLike=[[UIButton alloc] init]; [btnComLike setFrame:CGRectMake(127, 20, 30, 15)]; [btnComLike addTarget:self action:@selector(btnCommentLike_click:) forControlEve

我已经检查了许多文章,查看在ios中单击uibutton时传递值的情况。 但一般来说,人们使用标记属性作为替代

但我找到了以下解决方案:

 UIButton *btnComLike=[[UIButton alloc] init];

 [btnComLike setFrame:CGRectMake(127, 20, 30, 15)];
 [btnComLike addTarget:self action:@selector(btnCommentLike_click:) forControlEvents:UIControlEventTouchUpInside];
 [btnComLike setTitle:@"Like" forState:UIControlStateNormal];
 [btnComLike setTitle:@"my any string value" forState:UIControlStateReserved];
和处理方:

-(void)btnCommentLike_click:(id)sender
{
 NSLog(@"%@",[sender titleForState:UIControlStateReserved]);

}
这是可行的解决方案吗?在这里,我认为有人正在使用此状态
UIControlStateReserved

或者使用这种技术有什么缺点


如果是,请告诉我出了什么问题?

对我来说,这个解决方案就像殴打MVC和滥用糟糕的按钮

您应该只将要在action方法中使用的对象写入ivar(实例变量、成员变量)并在那里访问它

您的解决方案也可能被视为非法,具体如下:

uicontrol状态保留

保留供内部框架使用的控件状态标志


Yap,XY和设计问题,咕噜咕噜(一个人怎么可能在3行代码中犯两个巨大的概念错误?)。不要使用任何UIView的任何属性或字段(UIButton是UIView)来传递信息。这违反了Cocoa构建的模型-视图-控制器模式。顺便说一句:我看了你的个人资料:你应该接受更多的答案。