【发布时间】:2011-07-03 06:29:00
【问题描述】:
假设我有一个响应按钮事件的方法
- (void) myMethod: (id) sender
直接使用函数param更好吗?
NSLog(@"The sender tag is %d",sender.tag);
还是创建一个新对象更好?
UIButton* myButton = (UIButton*) sender;
NSLog(@"The sender tag is %d",myButton.tag);
为什么?
我在教程中看到,objective-c 中的首选方式是第二种方式。但是,如果您不需要知道发件人的类型而只需访问其属性/方法,则可以使用第一种方式。我错过了什么吗?
【问题讨论】:
-
这是风格问题,避免警告。
-
您没有在上面的任何代码中“创建新对象”。
UIButton* myButton = (UIButton*) sender;只是将sender中保存的地址复制到myButton。演员阵容甚至没有必要(除非您将某些深奥的编译器警告设置为暴躁的裤子)。
标签: iphone objective-c ios methods casting