【问题标题】:can't perform segue with prepare for segue method无法使用准备转场方法执行转场
【发布时间】:2013-09-13 11:38:56
【问题描述】:

您好,我正在尝试使用 prepare for segue 方法执行 segue。我正在关注这个问题this,但我无法成功注册。当我单击注册按钮时,它所做的只是注册用户并停在那里。我从视图控制器(而不是按钮本身)模态连接了我的segue,所以那个没有问题。我的发送视图只是一个带有文本字段和完成按钮的普通视图,而我的接收视图是一个静态表格视图,行上有一个标签(称为 nameLabel)。

2 个问题

1. 我的准备 segue 方法中缺少什么?(我也使用 parse 作为后端,所以这就是 PFLogInSuccessNotification 的来源)

  1. 如何将用户的用户名传递到我的个人资料视图?

感谢您抽出宝贵时间提供帮助。

**my segue method**

 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"can you see this");
    if ([segue.identifier isEqualToString:@"SegueTOprofile"])
    {
        if (PFLogInSuccessNotification) {
       profileTableViewController *myProfileView = (profileTableViewController *)[segue destinationViewController];

            PFUser *user =[PFUser user];
            user.username = myProfileView.nameLabel.text;
                   //   NSLog(@"%@",myProfileView.nameLabel.text);
    }

    }
    [self performSegueWithIdentifier:@"SegueTOprofile" sender:self];


}

**这是我的注册视图** .h

#import <UIKit/UIKit.h>
extern NSString *const PFSignUpSuccessNotification;
extern NSString *const PFSignUpFailureNotification;

@interface SignUpViewController : UIViewController<UITextFieldDelegate>


@property (strong, nonatomic) IBOutlet UITextField *emailNameField;
@property (strong, nonatomic) IBOutlet UITextField *passwordField;
@property (strong, nonatomic) IBOutlet UIButton *signupButton;
- (IBAction)SignUp:(id)sender;
@property (strong, nonatomic) IBOutlet UITextField *repeatPasswordField;
@property (strong, nonatomic) IBOutlet UIBarButtonItem *BackButton;
@property (strong, nonatomic) IBOutlet UITextField *usernameField;

.m

#import "SignUpViewController.h"
#import "profileTableViewController.h"
#import <Parse/Parse.h>
#import "ActivityView.h"
#import "TabBarCustomClassViewController.h"
@interface SignUpViewController ()

-(void)processFieldEntries;
- (void)textInputChanged:(NSNotification *)note;
- (BOOL)shouldEnableDoneButton;

@end

@implementation SignUpViewController
@synthesize signupButton = _signupButton;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];


    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textInputChanged:) name:UITextFieldTextDidChangeNotification object:_usernameField];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textInputChanged:) name:UITextFieldTextDidChangeNotification object:_emailNameField];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textInputChanged:) name:UITextFieldTextDidChangeNotification object:_passwordField];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textInputChanged:) name:UITextFieldTextDidChangeNotification object:_repeatPasswordField];


}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == _usernameField) {
        [_usernameField becomeFirstResponder];
    }
    if (textField == _emailNameField) {
  [_emailNameField becomeFirstResponder];}
    if (textField == _passwordField) {
        [_passwordField becomeFirstResponder];
    }
    if (textField == _repeatPasswordField) {
        [_repeatPasswordField becomeFirstResponder];
    }

    return  YES;
}

-(BOOL)shouldEnableDoneButton
{
    BOOL enableDoneButton = NO;
    if (_emailNameField.text != nil && _passwordField.text != nil && _repeatPasswordField != nil && _usernameField.text != nil) {
        enableDoneButton = YES;
    }
    return enableDoneButton;
}
/*
 // Using segue --> Send the current selected profile to "ProfileView"
 ProfileViewController *aProfileView = (ProfileViewController *)[segue destinationViewController];
 aProfileView.currentProfile = selectedProfile; */


 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"can you see this");
    if ([segue.identifier isEqualToString:@"SegueTOprofile"])
    {
        if (PFLogInSuccessNotification) {
       profileTableViewController *myProfileView = (profileTableViewController *)[segue destinationViewController];

            PFUser *user =[PFUser user];
            user.username = myProfileView.nameLabel.text;
                   //   NSLog(@"%@",myProfileView.nameLabel.text);
    }

    }
    [self performSegueWithIdentifier:@"SegueTOprofile" sender:self];


}



-(void)textInputChanged:(NSNotification *)note
{
    _signupButton.enabled = [self shouldEnableDoneButton];

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)SignUp:(id)sender {
    [_usernameField resignFirstResponder];
    [_emailNameField resignFirstResponder];
    [_passwordField resignFirstResponder];
    [_repeatPasswordField resignFirstResponder];
    [self processFieldEntries];
}
-(void)processFieldEntries
{
    // Check that we have a non-zero username and passwords.
    // Compare password and passwordAgain for equality
    // Throw up a dialog that tells them what they did wrong if they did it wrong.
    NSString *email = _emailNameField.text;
    NSString *passwordd = _passwordField.text;
    NSString *passwordAgainn = _repeatPasswordField.text;
    NSString *username = _usernameField.text;

    NSString *errorText = @"Please ";
    NSString *usernameBlankText = @"enter a username";
    NSString *passwordBlankText = @"enter a password";
    NSString *joinText = @", and ";
    NSString *passwordMismatchText = @"enter the same password twice";


    BOOL textError = NO;

    if (email.length == 0 || passwordd.length == 0 || passwordAgainn.length == 0) {
        textError = YES;

        // Set up the keyboard for the first field missing input:
        if (passwordAgainn.length == 0) {
            [_repeatPasswordField becomeFirstResponder];
        }
        if (passwordd.length == 0) {
            [_passwordField becomeFirstResponder];
        }
        if (username.length == 0) {
            [_usernameField becomeFirstResponder];
        }

        if (email.length == 0) {
            errorText = [errorText stringByAppendingString:usernameBlankText];
        }

        if (passwordd.length == 0 || passwordAgainn.length == 0) {
            if (email.length == 0) { // We need some joining text in the error:
                errorText = [errorText stringByAppendingString:joinText];
            }
            errorText = [errorText stringByAppendingString:passwordBlankText];
        }
    } else if ([passwordd compare:passwordAgainn] != NSOrderedSame) {
        // We have non-zero strings.
        // Check for equal password strings.
        if (_signupButton.enabled == YES) {
            textError = YES;
            errorText = [errorText stringByAppendingString:passwordMismatchText];

        }
    }
    if (textError) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:errorText message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alertView show];
        return;
    }

    // Everything looks good; try to log in.
    // Disable the done button for now.
    _signupButton.enabled = NO;
    ActivityView *activityView = [[ActivityView alloc] initWithFrame:CGRectMake(0.f, 0.f, self.view.frame.size.width, self.view.frame.size.height)];

    UILabel *label = activityView.label;
    label.text = @"Signing You Up";
    label.font = [UIFont boldSystemFontOfSize:20.f];
    [activityView.activityIndicator startAnimating];
    [activityView layoutSubviews];

    [self.view addSubview:activityView];

    // Call into an object somewhere that has code for setting up a user.
    // The app delegate cares about this, but so do a lot of other objects.
    // For now, do this inline.

    PFUser *user = [PFUser user];

    user.email = email;
    user.password = passwordAgainn;
    user.username = username;
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (error) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:[[error userInfo] objectForKey:@"error"] message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
            [alertView show];
            _signupButton.enabled = [self shouldEnableDoneButton];
            [activityView.activityIndicator stopAnimating];
            [activityView removeFromSuperview];
            // Bring the keyboard back up, because they'll probably need to change something.
            [_emailNameField becomeFirstResponder];

            return;
        }

        // Success!
        [activityView.activityIndicator stopAnimating];
        [activityView removeFromSuperview];




}];
}
@end

【问题讨论】:

    标签: ios objective-c storyboard segue parse-platform


    【解决方案1】:

    prepareForSegue 不执行 segue,它是您可以将数据传递到目标视图控制器的地方。如果您已从控制器(而不是按钮)连接 segue,则必须使用 performSegueWithIdentifier:sender: 手动调用 segue:

    如果你打算用这行来触发它:

    [self shouldPerformSegueWithIdentifier:@"SegueTOprofile" sender:self];
    

    应该是:

    [self performSegueWithIdentifier:@"SegueTOprofile" sender:self];
    

    我不知道这是否是您的意图,因为您在代码中使用了 2 个不同的 segue 标识符。

    【讨论】:

    • 我用带有标识符的执行序列更新了问题,但我没有看到我放在控制台中的 NSLog 所以..
    • @mett,您不会从 prepareForSegue 中调用 performSegueWithIdentifier ——由于触发了 segue,因此调用了 prepareForSegue。您需要将其添加到按钮方法或类似的方法中(我以为您想从 SignUp 中调用它:。如果是这样,请将其放在那里)。
    • 我相信我的逻辑错了。如果我只是用 [user signUpInBackgroundWithBlock:...] 选项保存用户并使用 parse 注册然后执行 segue,然后为用户查询信息并从那里进行设置?
    • 或者我应该坚持使用 segue 执行并从那里传递数据以进行注册?
    • @mett,我无法回答这个问题,因为我从未使用过 Parse。
    【解决方案2】:

    这里是如何使用 parse.com 作为后端实现登录过程的简洁示例。这最终是我需要的,所以我也在这里发布。

    https://github.com/gfosco/AccountDemo1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 2015-03-23
      • 2019-05-30
      • 2012-01-24
      相关资源
      最近更新 更多