Xcode - segue 的推送视图问题

Xcode - issue of push view by segue

我想使用 push segue 来改变视图。另外,我想将一些数据传递到目标视图。在 segue 更改视图之前,我想弹出一个带有文本字段的警报视图来检查用户密码。如果匹配,它将改变视图。如果没有,它将停止推送 segue。

我知道如果要阻止push segue做某事首先需要使用下面的方法,但是它无法得到segue.destinationViewController。反正可以代替segue.destinationViewController?

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

此外,我可以在 - (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender 方法中获取 alertView 文本字段结果吗?

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

有人可以帮助我吗?谢谢!

而不是在 shouldPerformSegueWithIdentifier 中创建警报,您应该在其他地方创建它(无论调用您的推送)并根据该警报的结果操作执行 [self performSegueWithIdentifier:@"yourIdentifier" sender:self];

要处理您要使用 UIAlertViewDelegate 和方法 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 的警报结果。

可能看起来像这样...

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