【问题标题】:Accessing a button of a table view from other view从其他视图访问表视图的按钮
【发布时间】:2011-06-13 12:13:21
【问题描述】:

如何从任何其他视图访问视图中存在的按钮。让我详细说明一下,我有两个视图控制器 A 和 B。在视图 A 中,我在表视图中有一个按钮,比如 button1。我想访问从我的另一个视图中的表格视图中的那个按钮。请帮助。我尝试创建 A 类的对象,然后编写 [objA.tableview button1 然后我设置图像],但它没有工作。请帮助。任何帮助将不胜感激。

谢谢, 克里斯蒂

【问题讨论】:

    标签: objective-c xcode ipad uitableview object


    【解决方案1】:

    你可以试试这个给你的按钮一个标签(确保它是唯一的)。

    假设您的按钮有标签 45;你已经在你的表格视图中做到了。

    现在你想用另一种方法来使用你的那个按钮:-

    -(void)deletePostMethod
    {
        UIButton *button=(UIButton *)[self.view viewWithTag:49];
        [button setBackgroundColor:[UIColor redColor]];
        NSLog(@"button=%@",button);
    }
    

    编辑答案:-

    - (UITableViewCell *)tableView:(UITableView *)tablefirst cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        // I  have added a button in my table view in first row
        if (indexPath.row==0) {
            UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
            [btn setFrame:CGRectMake(0, 0, 40, 30)];
            [btn addTarget:self action:@selector(deletePostMethod) forControlEvents:UIControlEventTouchUpInside];
            btn.tag=579;
            [cell.contentView addSubview:btn];
        }
            return cell;
    }
    

    关于那个按钮的点击方法

    -(void)deletePostMethod
    {
        UIButton *button=(UIButton *)[self.view viewWithTag:579];
        [button setBackgroundColor:[UIColor redColor]];
        NSLog(@"button=%@",button);
    }
    

    点击此按钮后按钮背景颜色设置为红色。

    【讨论】:

    • @christina 希望对你有帮助
    • 我无法理解请详细说明,我在一个视图中有一个表格视图,在另一个视图中有一个表格视图我如何从另一个视图中访问表格视图中预设的 uibutton查看
    • 你只需要创建一个带有唯一标签的按钮,假设你已经在你的 1table 视图中创建了。现在,无论您想在哪里访问该按钮,请使用此行 UIButton *button=(UIButton *)[self.view viewWithTag:49];它将引用您的那个按钮。现在您可以在按钮中做任何您想做的事情,例如图像更改或其他操作。
    • 我必须写 self.view 还是 myclassname.view
    • 或者更确切地说是表格视图,因为该按钮位于表格视图中
    【解决方案2】:

    使用viewControllers 属性访问导航堆栈中所有UIViewController 的列表。

    @property(nonatomic, copy) NSArray *viewControllers
    
    NSArray* allController = self.navigationController.viewControllers ;
    
    for(UIViewController* myAController in  allController)
    {
             if( [myAController isMemberOfClass])
             {
                  //Here Now you have the A controller 
                  MyViewAController*  myATempController =  (MyViewAController*)myAController;
    
                 //Now you could access the property of your A Controller.
             }
    }
    

    【讨论】:

      【解决方案3】:

      我会让你的按钮成为视图控制器的属性,这样你就可以通过名称访问它。

      在你的接口文件中:

      @interface MyViewController : UIViewController {
      
          UIButton* myButton;
      
      }
      
      @property (nonatomic, retain) UIButton* myButton;
      
      @end
      

      在你的实现文件中:

      @implementation MyViewController
      
      @synthesize myButton;
      
      @end
      

      然后你可以使用aViewController.myButton 来获取你的按钮。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-24
        • 1970-01-01
        • 1970-01-01
        • 2014-02-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多