Xcode - 由segue

时间:2015-07-28 20:06:15

标签: ios objective-c xcode uistoryboardsegue

我想使用push segue来改变视图。另外,我想将一些数据传递到目标视图。在segue更改视图之前,我想使用textfield弹出alertview来检查用户密码。如果匹配,则会更改视图。如果没有,它将停止推送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;
}

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

1 个答案:

答案 0 :(得分:1)

不是在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;
    }
}