create manual segue and config in viewController

regular segue from storyborad

In StoryBoard we can easily create segue by control drag the source widget to the destination widget.

Then in the viewContoller, just create a method:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"theSegueNameInStoryBoard"]) {
        NSIndexPath *indexPath = [self.searchTableView indexPathForSelectedRow];
        DestinationViewController *destViewController = segue.destinationViewController;
        NSString *parameterToPass= "@someparam";
        destViewController.param = parameterToPass;
    }
}

if the source widget is a cell in table view, use the indexPath to get the param from context.

if there is some simple condition to determine whether the segue to trigger or not, use:

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    if(something){
        return YES;
    }else{
        return NO;
    }
}

Manual segue

if the requirement contains determine the Yes or no in a alert, then the previous way will not work since it won’t wait for the user click and trigger the segue immediately.

Then we need to create segue ourselves:

manual-segue

manual-segue

A popup will show for “Manual Segue”. I picked Push as the type. Tap on the little square and make sure you’re in the attributes inspector. Give it an identifier which you will use to refer to it in code.

Next in the button action or table row selected action, we define the actual behavior:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self closeDrawer];

    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];
    NetworkStatus status = [reachability currentReachabilityStatus];
    switch (status) {
        case NotReachable:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"没有网络连接"
                                                            message:@"必须有wifi或者3g网络才能观看视频!"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
            break;
        }
        case ReachableViaWWAN:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"没有连接WIFI"
                                                            message:@"观看视频比较耗费流量,确定用3G继续吗?"
                                                           delegate:self
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"继续", nil];
            [alert show];
            break;
        }
        case ReachableViaWiFi:
        {
            [self performSegueWithIdentifier:@"theSegueNameInStoryBoard" sender:self];
            break;
        }

    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex != alertView.cancelButtonIndex){
        [self performSegueWithIdentifier:@"theSegueNameInStoryBoard" sender:self];
    }
}

More detail can be found here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s