I recently tried working with the MainStoryboard.storyboard within Xcode and so far It's going pretty good and I'm wondering why I've never used it before. While playing with some code I bumped into an obstacle and I don't know how to resolve this.
When I alloc and init a new ViewController (with a custom init I declared in the ViewControllers class) I would do something like this:
ViewController *myViewController = [[ViewController alloc] initWithMyCustomData:myCustomData];
Then after that I could do something like:
[self presentViewController:myViewController animated:YES completion:nil];
When I'm working with a storyboard I'm learnt that switching to a standalone ViewController requires an Identifier.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:#"MyViewControllerIdentifier"];
[self presentViewController:myViewController animated:YES completion:nil];
How can I still use my custom initialization for myViewController while making use of a storyboard?
Is it ok to just do something like this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
ViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:#"MyViewControllerIdentifier"];
myViewController.customData = myCustomData;
[self presentViewController:myViewController animated:YES completion:nil];
//MyViewController.m
- (id) initWithMyCustomData:(NSString *) data {
if (self = [super init]) {
iVarData = data;
}
return self;
}
I would just create a method which does the custom data loading.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
MyViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:#"MyViewControllerIdentifier"];
[myViewController loadCustomData:myCustomData];
[self presentViewController:myViewController animated:YES completion:nil];
If all your initWithCustomData method does is set one instance variable, you should just set it manually (no custom inits or extra methods required):
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
MyViewController *myViewController = [storyboard instantiateViewControllerWithIdentifier:#"MyViewControllerIdentifier"];
myViewController.iVarData = myCustomData;
[self presentViewController:myViewController animated:YES completion:nil];
You can instantiate viewcontroller in -init method.
- (instancetype)init
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
self = [storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
if(self)
{
//default initialization
}
return self;
}
and the in your custom init method
- (instancetype)initWithImages:(NSArray *)images
{
self = [self init];
if(self)
{
self.images = images;
}
return self;
}
My version:
- (instancetype)initWithData (NSArray *)someData
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
self = [storyboard instantiateViewControllerWithIdentifier:#"MyViewController"];
if(self)
{
//default initialization
}
return self;
}
...one initializer ;)
Related
I am trying to present a view controller in a tvOS application, but neither of the included code snippets present one. What am I missing?
Code 1 :
DinoViewController *dinoVC = [[DinoViewController alloc]init];
dinoVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:dinoVC animated:YES completion:nil];
Code 2 :
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
DinoViewController *dinoVC = [storyboard instantiateInitialViewController];
dinoVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:dinoVC animated:YES completion:nil];
Found the correct answer: Mention Identifier in the tvOS storyboard, (not the same in iOS,) and then implement this code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *dinoVC = [storyboard instantiateViewControllerWithIdentifier:#"Page1"];
[self presentViewController:dinoVC animated:YES completion:nil];
Please try this..
DinoViewController *dinoVC = [[DinoViewController alloc]init];
dinoVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self showViewController:dinoVC sender:nil];
hope this will hope u
I've written the code below to present a modal UIViewController on top of a UISplitViewController from AppDelegate. However, it keeps throwing Application tried to present modally an active controller <UISplitViewController: 0x138023800>..
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
NSLog(#"%#", shortcutItem.type);
if([shortcutItem.type isEqualToString:#"com.myapp.show-me"]){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UISplitViewController *tc = [storyboard instantiateViewControllerWithIdentifier:#"SplitViewController"];
self.window.rootViewController = tc;
UINavigationController *ivc = [storyboard instantiateViewControllerWithIdentifier:#"Navigation"];
UIViewController *detailViewController = [ivc.viewControllers objectAtIndex:0];
detailViewController.restaurant = #"1";
[tc presentViewController:detailViewController animated:YES completion:nil];
} else if([shortcutItem.type isEqualToString:#"com.myapp.see-all"]){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UISplitViewController *tc = [storyboard instantiateViewControllerWithIdentifier:#"SplitViewController"];
self.window.rootViewController = tc;
}
}
What am I doing wrong with com.myapp.show-me?
As it turns out, I only needed to present the UINavigationController instead of the UIViewController.
[tc presentViewController:ivc animated:YES completion:nil];
I have a simple code to take an integer and pass it to the next view but the int always ends up as 0 on the other view. I know it's not a problem with the variable because it turns out as the real int on the main view. Here is my code:
AnswerViewController *ansViewController = [[AnswerViewController alloc] initWithNibName:#"AnswerViewController" bundle:nil];
ansViewController.num = theNum;
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *mainView = [storyBoard instantiateViewControllerWithIdentifier:#"ansView"];
[self presentViewController:mainView animated:YES completion:nil];
Here is the right way to it:
//Initialize the storyboard
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
//initialize the ViewController
AnswerViewController *ansViewController =
[storyBoard instantiateViewControllerWithIdentifier:#"AnswerViewController"];
//Assign the value to the controller property
ansViewController.num = theNum;
//Finally present the view Controller
[self presentViewController:ansViewController animated:YES completion:nil];
I'm trying to switch back to a UITabBarController at a specific index. My method does not appear to be working. Here is my code:
BaseViewController.m
-(void)switchTabView: (NSString*) viewName inx: (int) index
{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UITabBarController *view = [storyBoard instantiateViewControllerWithIdentifier:viewName];
view.tabBarController.selectedIndex = index;
[self presentViewController:view animated:YES completion:nil];
}
ChildViewController.m (inherits BaseViewController)
- (IBAction)backButtonClicked:(id)sender
{
[self switchTabView:#"tabView" inx:1];
}
It goes back to the UITabBarController, but the index argument does not seem to matter since it always goes to index 0.
Try to change
-(void)switchTabView: (NSString*) viewName inx: (int) index
{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UITabBarController *view = [storyBoard instantiateViewControllerWithIdentifier:viewName];
view.tabBarController.selectedIndex = index;
[self presentViewController:view animated:YES completion:nil];
}
to
-(void)switchTabView: (NSString*) viewName inx: (int) index
{
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:#"Main_iPhone" bundle:nil];
UITabBarController *view = [storyBoard instantiateViewControllerWithIdentifier:viewName];
view.selectedIndex = index;
[self presentViewController:view animated:YES completion:nil];
}
-(void) viewDidAppear:(BOOL)animated {
if (!self.hasPresentedModalMenuView) {
NSLog(#"Firing");
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"highScoresStory" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"highScores"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:vc animated:YES completion:nil];
} }
This Keeps Repeating over and over again. I have taken out the code below "Firing", and if I do that, it only fires once. Why does this keep firing? Thank You for any help.