【问题标题】:How show tag of button?如何显示按钮的标签?
【发布时间】:2011-10-10 21:23:20
【问题描述】:

在下面的代码中,我想在控制台上显示每个按钮的标签。我尝试过,但它显示超出范围。怎么做?

 - (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);

}} 

【问题讨论】:

    标签: iphone xcode uiview uiscrollview uibutton


    【解决方案1】:

    在创建按钮时,在本地创建它(在 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)
    }
    

    【讨论】:

      【解决方案2】:
      -(IBAction)btnSelected:(id)sender{
      
      UIButton *btnSelected = sender;
      
      NSLog(@"%d",btnSelected.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);
      } 
      

      【讨论】:

      • 它给出了这个错误:- 在不是结构或联合的东西中请求成员“标签”
      • 你能告诉我我可以为图像视图执行此操作吗?你能看到我这个问题吗:stackoverflow.com/questions/6786170/…
      • 您可以直接在 UIButton 中使用图像,就像您在这篇文章中所做的那样,但您也可以避免使用大量 UIView,每个 UIButton 一个。你为什么不直接将 UIButtons 添加到你的 scool 中呢? [滚动添加子视图:btnMenu];并删除所有 UIView (如果您不需要其他原因...)
      • 如果我想让你使用简单的图像视图而不是带有图像的按钮,那么我如何实现所有操作?以及如何在每个图像上添加滚动视图,以便图像可以水平滚动。
      【解决方案4】:

      尝试将此代码作为您的操作方法

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

      }}

      这会打印当前选中按钮的标签。

      TNQ

      【讨论】:

      • 错误:在不是结构或联合的东西中请求成员“标签”
      • 如果你只想打印当前按钮的标签,你甚至可以删除 switch 语句。只在操作方法中放置日志语句
      【解决方案5】:
      -(IBAction)btnSelected:(id)sender{
          UIButton* btnMenu = (UIButton*)sender;
          switch (btnMenu.tag) {
          NSLog(@"%d",btnMenu.tag);
      
          }
      } 
      

      【讨论】:

      • 尝试使用 UIView 而不是 UIButton 可能是您为两个组件设置了相同的标签
      猜你喜欢
      • 2017-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多