简体   繁体   English

如何检查是否在iPhone应用程序中单击了任何按钮

[英]how to check that any of the button is clicked or not in iphone application

I have six button i want that user must select any of the six button before moving to next screen if it does not select any then it should not move to next screen I have read to use BOOL flag but this show which button is clicked i want that if any of the six buttons is clicked then user can move to next screen 我有六个按钮,我希望用户必须先选择六个按钮中的任意一个,然后再移至下一个屏幕(如果未选择),则不应移至下一个屏幕。如果单击六个按钮中的任何一个,则用户可以移至下一个屏幕

-(IBAction)locationOneButtonAction{
    // your stuff
    _flag = YES;
  }
 -(IBAction)locationTwoButtonAction{
// your stuff
_flag = YES;
 }

I think the best solution this problem is to declare a bool for your class (let's say it is called "ClickViewController" like (so at the top) 我认为这个问题的最佳解决方案是为您的类声明一个布尔值(假设它被称为“ ClickViewController”,例如(所以在顶部)

@interface ClickViewController () {
   bool wasClicked;
}
...
-(IBAction)locationOneButtonAction{
// your stuff
   wasClicked = YES;
}

Then when the button that should move to the next page is clicked utilize a method like this. 然后,当单击应移至下一页的按钮时,请使用类似的方法。

-(void)nextPageClicked{
   if (wasClicked){
       [self performSegueWithIdentifier:@"segueIdentifier" sender: self];
   } else {
      // do something else here, like tell the user why you didn't move them
   }
}

This means that you cannot draw the segue from the button to the next view. 这意味着您无法将按钮从按钮绘制到下一个视图。 If you are using storyboards, this means that you can't draw the segue from the button that is supposed to move it to the next view, rather you should draw it from the view controller icon to the next view. 如果使用情节提要板,则意味着您无法从应该将其移动到下一个视图的按钮上绘制序列,而应该从视图控制器图标将其绘制到下一个视图。 Then select the segue, name it, and use the method above with the name. 然后选择segue,为其命名,并使用上面的方法命名。

I'm also not sure if you mean that you want each of the buttons to move to the next view, if that is the case, you can use basically the same code, but just put the code [self performSegueWithIdentifier:@"segueIdentifier" sender: self]; 我也不确定您是否要让每个按钮移至下一个视图,如果是这样,则可以使用基本相同的代码,而只需将代码[self performSegueWithIdentifier:@“ segueIdentifier”发件人:自我]; line in action for the button. 该按钮的实际作用。

Hopefully this helps. 希望这会有所帮助。

Intially add BOOL _flag; 最初添加BOOL _flag; int curIndex; int curIndex; in .h file. 在.h文件中。

Now add intially _flag = FALSE; 现在添加_flag = FALSE; in viewDidLoad method as it indicate no button clicked. 在viewDidLoad方法中,因为它指示未单击任何按钮。

Make userInteractionEnabled for each button to be able to click and add tag to each button. 使每个按钮的userInteractionEnabled能够单击并将标记添加到每个按钮。

Now in action method of each click of button do this: 现在,在每次单击按钮的操作方法中执行以下操作:

(IBAction)locationOneButtonAction:(id)sender{
   curIndex = [sender tag]; //which button clicked
   // your stuff
   _flag = TRUE;
}
-(IBAction)locationTwoButtonAction{
curIndex = [sender tag]; //which button clicked
// your stuff
_flag = YES;
}
........
........

Now check like this if button clicked: 现在检查是否单击了按钮,如下所示:

if(_flag)
{
   NSLog(@"Button clicked with tag: %d",curIndex);
}
else
{
  NSLog(@"Not any Button clicked");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM