2011-07-22 60 views
0

在下面的代码中,我想显示控制台上每个按钮的标签。我为此尝试过,但显示范围之外。那是怎么回事?如何显示按钮的标签?

- (void)loadView { 
[super loadView]; 
self.view.backgroundColor = [UIColor redColor]; 
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
scroll.pagingEnabled = YES; 
NSInteger numberOfViews = 33; 
[btnMenu setTag:0 ]; 
for (int i = 1; i < numberOfViews; i++) { 
    CGFloat yOrigin = i * self.view.frame.size.width; 
    UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)]; 
    //awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1]; 
    btnMenu = [UIButton buttonWithType:UIButtonTypeCustom]; 
    //NSData *data =UIImageJPEGRepresentation(, 1); 
    [btnMenu setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"page-%d.jpg",i]] forState:UIControlStateNormal]; 
    CGRect frame = btnMenu.frame; 
    frame.size.width=320; 
    frame.size.height=460; 
    frame.origin.x=0; 
    frame.origin.y=0; 
    btnMenu.frame=frame; 
    [btnMenu setTag:i]; 
    btnMenu.alpha = 1; 
    [btnMenu addTarget:self action:@selector(btnSelected:) forControlEvents:UIControlEventTouchUpInside]; 
    [awesomeView addSubview:btnMenu]; 

    [scroll addSubview:awesomeView]; 
    [awesomeView release]; 
} 
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height); 
[self.view addSubview:scroll]; 
[scroll release]; 

}

-(IBAction)btnSelected:(id)sender{ 
switch (btnMenu.tag) { 
     NSLog(@"%d",btnMenu.tag); 

}} 

回答

3

试试这个:

-(IBAction)btnSelected:(id)sender{ 
    UIButton *button = (UIButton *)sender; 
    int whichButton = button.tag; 
    NSLog(@"Current TAG: %i", whichButton); 
} 

编辑:

你真正需要的方法为IBAction为方法?

难道你不能用它作为无效的吗?

-(void)btnSelected:(id)sender{ 
    UIButton *button = (UIButton *)sender; 
    int whichButton = button.tag; 
    NSLog(@"Current TAG: %i", whichButton); 
} 
+0

它给这个错误: - 请求成员'标记'的东西不是结构或联合 – ram

+0

在这一行代码:int whichButton = button.tag; ?? – meronix

+0

你能告诉我我可以为图像查看吗?你能看到我的这个问题:http://stackoverflow.com/questions/6786170/how-add-a-uibutton-on-every-image-of-image-view-which-is-in-scroll-view – ram

1
-(IBAction)btnSelected:(id)sender{ 
    UIButton* btnMenu = (UIButton*)sender; 
    switch (btnMenu.tag) { 
    NSLog(@"%d",btnMenu.tag); 

    } 
} 
+0

它没有响应。 – ram

+0

尝试使用UIView而不是UIButton可能是将相同的标记设置为两个组件 –

0

试试这个代码为您的操作方法

-(IBAction)btnSelected:(id)sender{ 
switch (sender.tag) { 
    NSLog(@"%d",sender.tag); 

}}

这种打印所选择的当前按钮的标签。

TNQ

+0

错误:请求成员'标记'的东西不是结构或联合 – ram

+0

如果您只想打印当前按钮的标记,您甚至可以删除switch语句。只在操作方法中的日志语句 – Dinakar

0

在创建您的按钮在本地(在for循环中)创建它,否则将只包含最后一个标签。

totalNoOfImages=32; 

为:

for(int i=0;i<totalNoOfImages;i++) 
{ 
    UIButton *button=[[[UIButton alloc] initWithFrame:CGRectMake(i*50,0,45,44)] autorelease];  

    //SETTING TAG FOR IMAGE 
    [button setTag:i]; 
    [button addTarget:self action:@selector(btnSelected:)forControlEvents:UIControlEventTouchDown]; 
    [scrollView addSubview:iconImageSlide]; 
} 

下面的方法将显示标签:

- (void) btnSelected:(id)sender 
{ 
    int btnId=[(UIButton *)sender tag]; 
    NSLog(@"btnTag= %d", ibtnId) 
} 
0
-(IBAction)btnSelected:(id)sender{ 

UIButton *btnSelected = sender; 

NSLog(@"%d",btnSelected.tag); 
} 

上面的代码工作对我来说,它是在日志打印标签值。你必须做错事。

+0

我检查它,然后我告诉和编辑我的代码。感谢你的支持。 – ram

相关问题