【问题标题】:Objective C - problem with passing an object name though method parameter when initializing objectObjective C - 初始化对象时通过方法参数传递对象名称的问题
【发布时间】:2011-10-23 22:53:50
【问题描述】:

我对 Objective C 非常陌生,我正在尝试创建一种方法来初始化一个对象(更准确地说是按钮对象),只需要一行代码......我的方法声明是......

- (void)buttonDeclaration: (UIButton *)mButton :(int)xloc :(int)yloc :(int)bWidth :(int)bHeight 
                         : (NSString *)sImage :(UIViewController *)mView :(SEL)mSelector
{
  mButton = [UIButton buttonWithType:UIButtonTypeCustom];
  [self buttonSetxy:mButton :xloc :yloc :bWidth :bHeight];
  [mButton setBackgroundImage:[UIImage imageNamed:sImage] forState:UIControlStateNormal];

  [mView.view addSubview:mButton];
}

我的方法调用是……

[...buttonDeclaration:newButton :40 :65 :80 :65...]

但是当我尝试添加时

[newButton setHidden:FALSE]; 

在我调用该方法后,它什么也不做。我不确定正确的术语是什么,但对象名称应该是 newButton 而不是 mButton。这有意义吗?我该如何做到这一点?

【问题讨论】:

  • buttonSetxy 方法是做什么的???您是否正确设置了框架?
  • buttonSetxy 只是另一种自定义方法,它只是改变了我的按钮的坐标。同样,对于我的主视图控制器中的单行编码。
  • 你能贴出buttonSetxy的代码吗? Cz 如果你不设置 btn 的框架,那么它不会被添加。

标签: objective-c object methods parameters


【解决方案1】:

实际上在objective c中声明方法的方式是不同的。

当你声明带有多个参数的方法时,它应该是这样的。

  • (void) myMethod : (int)firstNum secondArgument:(int)secondNum

所以你的方法将被声明为

  • (void)buttonDeclaration:(UIButton *)mButton xPosition:(int)xloc yPosition:(int)yloc Width:(int)bWidth height:(int)bHeight imageName:(NSString *)sImage myView:(UIViewController *) mView 选择器:(SEL)mSelector

现在你将调用这个方法

[self buttonDeclaration : myBtn xPosition : 5 yPosition : 10 width : 5 height : 10 等等......]

如果你想隐藏你的按钮,只需写

myBtn.hidden = YES;

【讨论】:

  • 这只是更改名称,但不会产生任何其他效果。不过,我同意使用未命名的参数是一件坏事。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-06
  • 2014-10-07
  • 1970-01-01
  • 2012-08-13
  • 2018-07-31
  • 1970-01-01
相关资源
最近更新 更多