简体   繁体   中英

Transitioning to view programmatically ios

I have a view that carries out a task, when the task is completed the activity indicator will become hidden, I want to send the user to another view when the activity is completed, or give the user an error if unsuccessful. So far I am using an If Else statement to give a success alert or an error alert. There is no buttons to click or anything, simply after the activity is completed the user will be sent to another view passing a few variables along the way.

How would I go about sending the user to another view after completion?

If you use a navigationcontroller:

 [self.navigationController pushViewController:theNextViewController animated:YES]; 

For Storyboard, look up the method in the docu.

To elaborate on AlexWien's answer a little...

  • Create a view controller or table view controller class that you want the user to go to after completion.
  • Create some properties for the data you want to pass in.

@protocol UpdatePricesDelegate;

@interface NXUpdatePricesViewController : UITableViewController

@property (strong, nonatomic)   NSArray *calculationProducts;
@property (strong, nonatomic)   NSArray *filteredCalculationProducts;

@property (weak, nonatomic)     id<UpdatePricesDelegate>delegate;

@end

@protocol UpdatePricesDelegate <NSObject>

- (void)updatePricesController:(NXUpdatePricesViewController *)controller didUpdateCalculationProducts:(NSArray *)calculationProducts;

@end

  • When you are ready to display your controller (presumably in your If/Else statement), instantiate the class (don't forget #import "MyClassName.h") and set the properties to the variables you want to pass.
  • Present the class modally (example includes a navigation controller), or if you want to push the view, use your navigation controller.

NXUpdatePricesViewController *updatePricesController = [[NXUpdatePricesViewController alloc] initWithStyle:UITableViewStyleGrouped];
updatePricesController.delegate = self;
updatePricesController.calculationProducts = self.calculationProducts;

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:updatePricesController];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;


[self.navigationController presentViewController:navigationController animated:YES completion:nil];

NXCalculationViewController *calculationController = [[NXCalculationViewController alloc] init];
calculationController.calculation = calculation;
[self.navigationController pushViewController:calculationController animated:YES];

I actually got it working, along with passing a variable to the other view using this method:

DetailViewController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailView"];
[detail setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
detail.videoURL = outputURL;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM