Presenting a view controller in tvOS - objective-c

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

Related

Present modal UIViewController over UISplitViewController from AppDelegate

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];

Passing int between views doesn't work

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];

How can i go to storyboard from xib

i am using both storyboard and xib in my project and i want to go xib files from storyboard but can't reverse it. so how i go to storyboard from xib files. thnx in advance.
storyboard to xib:
Dashboard1ViewController *tempView = [[Dashboard1ViewController alloc] initWithNibName:#"Dashboard1ViewController" bundle:nil];
tempView.passemail = [matches valueForKey:#"email"];
[self.view addSubview:tempView.view];
it works fine but xib to storyboard i have used this code
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"loginUI" bundle:nil];
login2ViewController *vc = [sb instantiateViewControllerWithIdentifier:#"login2"];
vc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:vc animated:YES completion:NULL];
but it is not working.
Change nil to [NSBundle mainBundle]
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"loginUI"
bundle:[NSBundle mainBundle]];
the following method is working for me. make sure that your identifier is correct
YourViewController *urViewController = (YourViewController *)[[UIStoryboard storyboardWithName:storyboardName bundle:nil] instantiateViewControllerWithIdentifier:#"storyboardID"];
[self presentViewController:urViewController animated:YES completion:nil];

Fires presentedViewController several times

-(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.

Storyboard and custom init

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 ;)