Iphone 按下UIB按钮后,如何打印此变量的值?

Iphone 按下UIB按钮后,如何打印此变量的值?,iphone,uibutton,Iphone,Uibutton,在上面的代码中,当我按下按钮时,我想在ShowNumber方法中打印col变量的值。如何执行此操作?Col是一个局部变量,因此在方法返回后会被销毁 您需要创建一个实例变量才能访问该值。不要使用普通局部变量,我建议不要使用int。使用NSInteger并将其设置为属性: // My Button code UIButton *ticketButtonObj=[[ticketButton alloc]initWithFrame:CGRectMake(0.0f, 115.0f, 500.0f, 40.

在上面的代码中,当我按下按钮时,我想在
ShowNumber
方法中打印col变量的值。如何执行此操作?

Col是一个局部变量,因此在方法返回后会被销毁


您需要创建一个实例变量才能访问该值。

不要使用普通局部变量,我建议不要使用int。使用
NSInteger
并将其设置为属性:

// My Button code
UIButton *ticketButtonObj=[[ticketButton alloc]initWithFrame:CGRectMake(0.0f, 115.0f, 500.0f, 40.0f) ;
int col=10;

[ticketButtonObj addTarget:self action:@selector(ShowNumber:)  forControlEvents:UIControlEventTouchUpInside];

[self.window addSubview:ticketButtonObj];
// ...   
- (void) ShowNumber:(id)sender{
    // here i want to get the Value of Col
}
然后
@合成
变量
col
,并在代码中的任意位置使用它。

您可以:

@interface YourClassName : UIViewController
{
    NSInteger col;
}
@property (nonatomic) NSInteger col;
UIButton *ticketButtonObj=[[ticketButton alloc]initWithFrame:CGRectMake(0.0f, 115.0f, 500.0f, 40.0f) ;
int col=10;
ticketButtonObj.tag = col;
[ticketButtonObj addTarget:self action:@selector(ShowNumber:)  forControlEvents:UIControlEventTouchUpInside];

[self.window addSubview:ticketButtonObj];

// ... 

- (void) ShowNumber:(id)sender{
NSLog(@"%@", [(UIButton*) sender tag]);}