简体   繁体   中英

What is the best way to differentiate between two methods going to same view controller?

This is a little complicated, but here is what I need to accomplish.
Basically, I have a ViewController that has two buttons (login and upload video button) that both goes to a login ViewController. However, when a user clicks on the upload video button and then the user signs in they need to be redirected to a third ViewController called uploadVideoViewController. Where as the user clicks on the login button ,he doesn't need to be redirected anywhere. Is there a way to differentiate between the two buttons so I can properly redirect the user when I need to?

ViewController

-(IBAction)uploadVideo:(id)sender
{
    [self loginViewController];
}

-(IBAction)login:(id)sender
{
    [self loginViewController];
}

LoginViewController

-(void)login 
{
    if (uploadVideobuttonclicked) {
       [self uploadViewController];
    } else {
        //do nothing
    }
}

I would use a custom initializer for your view controller. It would help if you could show the loginViewController method.

But I would do something like

[self loginViewControllerWithVideo:YES];

then when the 2nd view loads you can check for that bool and simply load the next view.

You could even go one step further and include the video instead of a bool value.

[self loginViewControllerWithVideo:videoToUpload];

Putting this together here is an example based on your code. Note that I removed self. I don't think self would be necessary in this case. If you need an example for the initializer let me know.

-(IBAction)uploadVideo:(id)sender
{
    [loginViewController loginViewControllerWithVideo:videoToUpload];
}

-(IBAction)login:(id)sender
{
    [loginViewController loginViewControllerWithVideo:nil];
}

将BOOL参数添加到-(void) login方法,并从两个不同的地方传递TRUE / FALSE。

I'm going to show an alternative design for the solution to your problem. The issue I see with your current design is that you are coupling the upload action and the login action too tightly. What if in the future you add another button that also requires login? You end up with a huge if and, what is worse, your loginViewController knows too much: It knows about an uploadViewController , when it shouldn't. It's only responsibility should be to log the user in, nothing more. We can use delegation to make your design more clean and extensible.

First, define a simple delegate to communicate your view controllers:

@protocol LoginDelegate <NSObject>
- (void)didEndLogin;
@end

Then, in your main view controller:

-(IBAction)uploadVideo:(id)sender
{
    [self.uploadViewController upload];
}

-(IBAction)login:(id)sender
{
    [self.loginViewController login];
}

Then, include the login logic in UploadViewController :

@class UploadViewController<LoginDelegate>

- (void)upload
{
    if (user is not logged in) {
        LoginViewController *loginViewController = [LoginViewController alloc] init...];
        loginViewController.delegate = self;
        // You can present this view controller as you like
        [self presentViewController:loginViewController animated:YES completion:nil];
    } else {
        // Upload
    }
}

- (void)didEndLogin
{
    // Upload
}

In your loginViewController , declare the delegate and call it after login is complete:

@property (nonatomic, weak) id<LoginDelegate> delegate;

- (void)login
{
    ...
    [self.delegate didEndLogin];
}

Is it just me but why didn't anyone suggest adding a tag to a UIButton. That way you know which button was pressed ... unless I missed OPs question.

- (void)viewDidLoad {
   UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
   button.tag = 500;

   [super viewDidLoad];
}

- (IBAction)login:(id)sender {

   if ([sender tag] == 500) {
        // do something
    }
}

Add a property to loginViewController @property(nonatomic) BOOL uploadButtonPressedInVC; Now when you are navigating to loginViewController set that property.

-(IBAction)uploadVideo:(id)sender
{    LoginViewController *lVC=[[LoginViewController alloc]init];
     lVC.uploadButtonPressedInVC=YES;
    [self loginViewController];

}

-(IBAction)login:(id)sender
{
     LoginViewController *lVC=[[LoginViewController alloc]init];
     lVC.uploadButtonPressedInVC=YES;
    [self loginViewController];
}

Or you can create LoginViewController object in your method definition.

This might help you:

ViewController

-(IBAction)uploadVideo:(id)sender
{
    [[NSUserDefaults standardUserDefaults] setBool:YES forKye:@"Video_To_Upload"];

    [self loginViewController];
}

-(IBAction)login:(id)sender
{
   [[NSUserDefaults standardUserDefaults] setBool:NO forKye:@"Video_To_Upload"];

   [self loginViewController];
}

LoginViewController

-(void)login 
{
    BOOL uploadVideobuttonclicked = [[NSUserDefaults standardUserDefaults]  boolForKey@"Video_To_Upload"];
    if (uploadVideobuttonclicked) {
       [self uploadViewController];
   } else {
      //do nothing
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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