如何在iPhone编程中制作切换图像按钮而不是UIBarButtonItem?

发布于 2024-12-07 11:44:45 字数 321 浏览 0 评论 0原文

我必须在 UINavigationBar 中创建一个按钮,该按钮应该在重新启动状态和取消状态之间切换,并且应该

   -(void)RestartMethod {}

在重新启动状态下调用,并且应该在 UIBarButtonItem 的取消状态下调用方法

    -(void) cancelMethod {}

,这两种状态都使用像 start.png 和 calcel.png 这样的图像

我尝试制作两个图像并添加和删除目标,但面临一些错误的执行问题, 我该怎么办? 帮助!

I have to make a button in UINavigationBar, the button should toggle between restart state and cancel state, and it should call

   -(void)RestartMethod {}

at restart state and should call method

    -(void) cancelMethod {}

at cancel state of UIBarButtonItem,both states are using images like start.png and calcel.png

I tried by making two images and add and remove targets,b but facing some bad-exec issues,
how can I do it?
Help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╰ゝ天使的微笑 2024-12-14 11:44:45

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;}

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;}
笑红尘 2024-12-14 11:44:45

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

Create two buttons with two different targets. When one button is clicked do whatever you want the action to be and replace the button with button number two. If button number two is clicked, replace that with button number 1.

瀟灑尐姊 2024-12-14 11:44:45

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

  1. 用布尔值声明初始状态

    BOOL buttonOn = NO;

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

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

Why not use a segment control instead of toggle button? Even if you insist on using a single button to toggle, this is how I would achieve it.

  1. Have a bool declare the initial state

    BOOL buttonOn = NO;

  2. Embed the first button with initial image pointing to one selector

  3. Inside selector take action based on this boolean. Change state and change this boolean's value too. No need to have two different selectors. Call appropriate functions to do stuff inside this selector.
生寂 2024-12-14 11:44:45

我认为这可以帮助你:

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];

新的 write 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
               }

        }

i think this can help you:

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];

New write backButtonPress: method:

        -(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
               }

        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文