Prevent segue in prepareForSegue method?

2019-01-04 05:38发布

Is it possible to cancel a segue in the prepareForSegue: method?

I want to perform some check before the segue, and if the condition is not true (in this case, if some UITextField is empty), display an error message instead of performing the segue.

10条回答
Anthone
2楼-- · 2019-01-04 06:18

Its easy in the swift .

override func shouldPerformSegueWithIdentifier(identifier: String,sender: AnyObject?) -> Bool {

    return true
}
查看更多
SAY GOODBYE
3楼-- · 2019-01-04 06:18

As Abraham said, check valid or not in the following function.

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(nullable id)sender
{
     // Check this identifier is OK or NOT.
}

And, the performSegueWithIdentifier:sender: called by programming can be blocked by overwriting following method. By default, it is not checking valid or not by -shouldPerformSegueWithIdentifier:sender:, we can do it manually.

- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    // Check valid by codes
    if ([self shouldPerformSegueWithIdentifier:identifier sender:sender] == NO) {
        return;
    }

    // If this identifier is OK, call `super` method for `-prepareForSegue:sender:` 
    [super performSegueWithIdentifier:identifier sender:sender];
}
查看更多
▲ chillily
4楼-- · 2019-01-04 06:23

Should Perform Segue for Login Register

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

    [self getDetails];

    if ([identifier isEqualToString:@"loginSegue"])
    {

        if (([_userNameTxtf.text isEqualToString:_uname])&&([_passWordTxtf.text isEqualToString:_upass]))
        {

            _userNameTxtf.text=@"";
            _passWordTxtf.text=@"";

            return YES;
        }
        else
        {
            UIAlertView *loginAlert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Invalid Details" delegate:self cancelButtonTitle:@"Try Again" otherButtonTitles:nil];

            [loginAlert show];

            _userNameTxtf.text=@"";
            _passWordTxtf.text=@"";

            return NO;
        }

    }

    return YES;

}

-(void)getDetails
{
    NSArray *dir=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *dbpath=[NSString stringWithFormat:@"%@/userDb.sqlite",[dir lastObject]];

    sqlite3 *db;

    if(sqlite3_open([dbpath UTF8String],&db)!=SQLITE_OK)
    {
        NSLog(@"Fail to open datadbase.....");
        return;
    }

    NSString *query=[NSString stringWithFormat:@"select * from user where userName = \"%@\"",_userNameTxtf.text];

    const char *q=[query UTF8String];

    sqlite3_stmt *mystmt;

    sqlite3_prepare(db, q, -1, &mystmt, NULL);

    while (sqlite3_step(mystmt)==SQLITE_ROW)
    {
        _uname=[NSString stringWithFormat:@"%s",sqlite3_column_text(mystmt, 0)];

        _upass=[NSString stringWithFormat:@"%s",sqlite3_column_text(mystmt, 2)];
    }

    sqlite3_finalize(mystmt);
    sqlite3_close(db);

}
查看更多
Evening l夕情丶
5楼-- · 2019-01-04 06:23

Similar to Kaolin's answer is to leave the seque wired to the control but validate the control based on conditions in the view. If you're firing on table cell interaction then you also need to set the userInteractionEnabled property as well as disabling the stuff in the cell.

For instance, I've got a form in a grouped table view. One of the cells leads to another tableView that acts as a picker. Whenever a control is changed in the main view I call this method

-(void)validateFilterPicker
{
    if (micSwitch.on)
    {
        filterPickerCell.textLabel.enabled = YES;
        filterPickerCell.detailTextLabel.enabled = YES;
        filterPickerCell.userInteractionEnabled = YES;
        filterPickerCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    else
    {
        filterPickerCell.textLabel.enabled = NO;
        filterPickerCell.detailTextLabel.enabled = NO;
        filterPickerCell.userInteractionEnabled = NO;
        filterPickerCell.accessoryType = UITableViewCellAccessoryNone;
    }

}
查看更多
淡お忘
6楼-- · 2019-01-04 06:30

Swift 3: func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool

Return value true if the segue should be performed or false if it should be ignored.

Example:

var badParameters:Bool = true

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    if badParameters  {
         // your code here, like badParameters  = false, e.t.c
         return false
    }
    return true
}
查看更多
▲ chillily
7楼-- · 2019-01-04 06:33

Swift 4 Answer:

Following is Swift 4 implementation to cancel segue:

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    if identifier == "EditProfile" {
        if userNotLoggedIn {
            // Return false to cancel segue with identified Edit Profile
            return false
        }
    }
    return true
}
查看更多
登录 后发表回答