JacksonNg
JacksonNg

Reputation: 53

Xcode - issue of push view by segue

I want to use push segue to change view. Also, I want to past few data to destination view. Before the segue change view, I want to pop a alertview with textfield to check user password. If match, it will change the view. If not, it will stop the push segue.

I know that if want to prevent the push segue to do something first need to use the below method, but it cannot get the segue.destinationViewController. Is anyway can replace the segue.destinationViewController?

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender

Moreover, can I get the alertView textfield result in the - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender method?

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
   if ([identifier isEqualToString:@"chatSegue"]) {
       NSLog(@"should");
       NSString *plock = [Server getUserDatawithkey:@"plock"];
       if ([plock isEqual:@"1"]) {

           UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Privacy Lock" message:@"Please enter your password:" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];
           alert.alertViewStyle = UIAlertViewStylePlainTextInput;
           UITextField * addFriendField = [alert textFieldAtIndex:0];
           addFriendField.keyboardType = UIKeyboardTypeDefault;
           addFriendField.placeholder = @"Enter your password";
           alert.tag = ALERT_TAG_PW;
           [alert show];
           // Can I get the alertView textfield result at here?

           NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
           friendCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

           // Is anyway to replace the segue.destinationViewController?
           chatViewController *cvc = segue.destinationViewController;
           cvc.fid = cell.fid.text;
           cvc.title = cell.f_name.text;
           return YES;
       }
       return NO;
   }
   return YES;
}

Is somebody can help me? Thanks!

Upvotes: 0

Views: 79

Answers (1)

TheSD
TheSD

Reputation: 883

Instead of creating the alert in shouldPerformSegueWithIdentifier you should create it somewhere else (whatever is calling your push) and depending on the resulting action of that alert perform [self performSegueWithIdentifier:@"yourIdentifier" sender:self];

To handle the result of your alert you want to use the UIAlertViewDelegate and the method - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex.

Might look something like this...

- (void)getServerStuff {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Privacy Lock" message:@"Please enter your password:" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];
    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * addFriendField = [alert textFieldAtIndex:0];
    addFriendField.keyboardType = UIKeyboardTypeDefault;
    addFriendField.placeholder = @"Enter your password";
    alert.tag = ALERT_TAG_PW;
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(alertView.tag == ALERT_TAG_PW) {
        if(buttonIndex == 0) {
          [self performSegueWithIdentifier:@"pushViewIdentifier" sender:self];
        }
        else if (buttonIndex == 1) {
          //Do Nothing
        }
    }
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"pushViewIdentifier" sender:self]) {
        ChatViewController *cvc = (ChatViewController *)segue.destinationViewController;
        cvc.property = self.someProperty;
    }
}

Upvotes: 1

Related Questions