1
// 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
}

In the above code when I press the button, I want to print the value of col variable in the ShowNumber method. How can I do this?

4

3 回答 3

0

Col is a local variable and is therfore destroyed after the method returns.

you need to create a instance variable for you to be able to access the value.

于 2009-06-05T04:41:15.407 回答
0

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

@interface YourClassName : UIViewController
{
    NSInteger col;
}
@property (nonatomic) NSInteger col;

然后@synthesize是变量col并在代码中您喜欢的任何地方使用它。

于 2012-01-17T18:08:22.850 回答
0

你可以:

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]);}
于 2013-01-30T15:38:04.843 回答