【问题标题】:How to make toggle image button instead of UIBarButtonItem in iphone programming?如何在 iphone 编程中制作切换图像按钮而不是 UIBarButtonItem?
【发布时间】:2011-09-30 06:25:11
【问题描述】:

我必须在 UINavigationBar 中创建一个按钮,该按钮应该在重启状态和取消状态之间切换,它应该调用

   -(void)RestartMethod {}

在重启状态,应该调用方法

    -(void) cancelMethod {}

在 UIBarButtonItem 的取消状态下,两种状态都使用 start.png 和 calcel.png 之类的图像

我尝试制作两个图像并添加和删除目标,但遇到了一些糟糕的执行问题, 我该怎么做? 救命!

【问题讨论】:

    标签: iphone objective-c uinavigationbar uibarbuttonitem


    【解决方案1】:

    item1=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"DoneUp3.png"] style:UIBarButtonItemStylePlain target:self action:@selector(action1)]; item2=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"Pin.png"] style:UIBarButtonItemStylePlain target:self action:@selector(action2)];

    -(void)action1{self.navigationItem.rightBarButtonItem = item2;}
    -(void)action2{self.navigationItem.rightBarButtonItem = item1;}
    

    【讨论】:

      【解决方案2】:

      创建两个具有两个不同目标的按钮。当单击一个按钮时,执行您想要的任何操作,并将该按钮替换为第二个按钮。如果单击了第二个按钮,则将其替换为第 1 个按钮。

      【讨论】:

        【解决方案3】:

        为什么不使用段控件而不是切换按钮?即使您坚持使用单个按钮进行切换,我也是这样做的。

        1. 有一个 bool 声明初始状态

          BOOL 按钮打开 = 否;

        2. 嵌入第一个按钮,初始图像指向一个选择器

        3. 内部选择器根据此布尔值采取行动。更改状态并更改此布尔值。不需要有两个不同的选择器。调用适当的函数在此选择器中执行操作。

        【讨论】:

        • 很好,也给了你解决方案。试试看
        • 我知道这个解决方案,我在 Sudesh Kumar Ans 找到了它,也感谢您的回答
        【解决方案4】:

        我认为这可以帮助你:

        CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
        UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];   
        [someButton setBackgroundImage:[UIImage imageName:@"start.png"] forState:UIControlStateNormal];
        [someButton setBackgroundImage:[UIImage imageName:@"calcel.png"] forState:UIControlStateSelected];
        [someButton addTarget:self action:@selector(backButtonPress:)forControlEvents:UIControlEventTouchUpInside]; 
        UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
        self.navigationItem.leftBarButtonItem=mailbutton;
        [mailbutton release];
        

        新写backButtonPress:方法:

                -(void)backButtonPress:(id)sender{
                    UIButton *tmpButton = (UIButton *)sender;
                       tmpButton.selected = [tmpButton isSelected]?NO:YES;
        
                      if (tmpButton.selected) {
                            // call cancelMethod or write RestartMethod code here
                        }
                        else{
                            // call RestartMethod or write RestartMethod code here
                       }
        
                }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-20
          • 1970-01-01
          • 2015-04-16
          • 2023-03-10
          • 1970-01-01
          • 1970-01-01
          • 2014-02-07
          • 2021-09-11
          相关资源
          最近更新 更多