【问题标题】:Function with variable blind to a button xcode对按钮xcode视而不见的函数
【发布时间】:2011-01-18 12:26:12
【问题描述】:

您好,我有一个问题,我无法为函数发送参数

-(IBAction)ukazObrat:(NSInteger *)cis {     
    stvrtyViewController *screen = [[stvrtyViewController alloc] initWithNibName:nil bundle:nil];
    screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:screen animated:YES];
    screen.obratLabel.font = [UIFont systemFontOfSize: 12];
    screen.obratLabel.numberOfLines = 0;
    screen.obratLabel.text = [array objectAtIndex:cis];
    [screen release];   
}

我将此函数设置为一个按钮单击,但是当我向它添加参数时我无法编译它。

button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button.titleLabel setLineBreakMode:UILineBreakModeWordWrap];    
[button.titleLabel setTextAlignment:UITextAlignmentLeft];       
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button setTitle:obratLable forState:UIControlStateNormal];
[button addTarget:self 
           action:@selector([self ukazObrat:minus]
 forControlEvents:UIControlEventTouchDown];             
button.frame = CGRectMake(0.0, 0.0, 250.0, 60.0);

在我添加变量之前它一直在工作。 请帮忙

【问题讨论】:

    标签: objective-c cocoa-touch ios uibutton


    【解决方案1】:
    [button addTarget:self action:@selector(buttonHandler) forControlEvents:UIControlEventTouchDown];  
    

    然后将buttonHandler定义为:

    - (void)buttonHandler {
      [self ukazObrat:minus];
    }
    

    管理minus 是必要的。

    【讨论】:

      【解决方案2】:

      您不能将变量设置到选择器定义中。定义只能包含函数名。冒号的数量将显示编译器这个函数需要多少参数。按钮的选择器可以有一个参数 - 指向事件发送按钮的指针。如果您想将一些数据从一个控制器发送到另一个控制器,您应该为该数据创建一些共享存储。

      我为此使用带有可变字典的单例类。

      【讨论】:

      • 你应该在 nsobject 的类别中实际使用关联引用或静态可变字典。` - (NSMutableDictionary )sharedUserInfo {static NSMutableDictionary *sharedUserInfo = nil; if (sharedUserInfo) 返回sharedUserInfo; / 否则分配一个新的并设置 sharedUserInfo */} `
      • 我的班级也处理保存/加载到/从 NSKeyedArchive 所以它有点复杂,比你描述的要复杂。但主要思想是正确的。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多