在iOS中单击UIButton时传递字符串值
我已经检查了许多文章,查看在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
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构建的模型-视图-控制器模式。顺便说一句:我看了你的个人资料:你应该接受更多的答案。