开发者

Two "Choice" buttons, using same methods

开发者 https://www.devze.com 2023-02-24 05:19 出处:网络
So in my iPhone app I\'m building, I have two buttons that are going to be designated for \"Choice 1\" and \"Choice 2\" for the users to select their date and time (for ex. to schedule an appointment)

So in my iPhone app I'm building, I have two buttons that are going to be designated for "Choice 1" and "Choice 2" for the users to select their date and time (for ex. to schedule an appointment).

Choice 1 has an ActionSheet to display the date, and is set to where when the user taps "Selected", it automatically goes to the other method I have which displays two times that are programmed in (As NSArray classes).

What my question is, I want to keep those two methods, but use a method called "button1Clicked" calling both, and do the same for "button2Clicked". Any ideas? I'll be more than willing to supply code that I have existing. Thanks!

I couldn't find this already answered 开发者_如何学Cbut if someone knows if this is indeed answered on the site here can you supply a link? That would be appreciated!

Go easy please, I'm still a beginner :)


well, you can add a 'tag' property to each button, and then at the beginning of your first method, check the tag value of the sender so that you know which was clicked. That way you don't need to duplicate any code. Is that what you are trying to do, or did I misunderstand your goal?


Can you clarify your question. Are you asking how to write a method which contains calls to other methods? Or are you asking how to hook up a method to be called upon a button press? Or are you asking how to have both buttons call different methods, which methods then call the two first mentioned methods?

For one method to handle multiple buttons:

Approach A:

- (IBAction)onSomeClick:(id)sender {

    // return if sender is not a button
    if (![sender isKindOfClass:[UIButton class]])
        return;

    NSString *title = [(UIButton *)sender currentTitle];
    if(title == @"Choice 1")
        // call choice 1 methods here
    else if(title == @"Choice 2"
        // call choice 2 methods here
}

Approach B:

-(IBAction)onButtonPress:(id)sender{ 
     // if you have the logic for differentiating between 
     // the sender in the showActionSheet method, no need
     // for it here, just send the sender as the param
     [self showActionSheet:sender];         

     // call another method here also if appropriate
     // .. or as needed call it from within the showActionSheet method
}

// note how this does not need the IBAction return. This method doesn't need to be 
// hooked up via Interface Builder
-(void)showActionSheet:(id)sender{
     // here you can do your differentiation logic if you need it
}
0

精彩评论

暂无评论...
验证码 换一张
取 消