Need help converting this bit of code from Swift to Objective-C - objective-c

I have an Objective-C app where I need to load another VC class from within the code. I've managed to accomplish this in another Swift app.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "OverrideClass")
self.present(controller, animated: true, completion: nil)
I need help creating an identical function for ObjC. It's basically loading the class of the VC with the storyboard identifier mentioned in the code.

The code is very similar just with a different syntax...
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:NULL];
UIViewController *controller = [storyboard instantiateViewControllerWithIdentifier:#"OverrideClass"];
[self presentViewController:controller animated:YES completion:NULL];

Here's the code you provided re-written in Objective-C:
UIStoryboard * storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:NULL];
UIViewController * controller = [storyboard instantiateViewControllerWithIdentifier:#"OverrideClass"];
[self presentViewController:controller animated:YES completion:NULL];

Related

Presenting a view controller in tvOS

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

presenting UITableViewController blocks UI

i'm trying to present a UITableViewController when a collectionViewCell is tapped. i've done this by using the didSelectItemAtIndexPath. When i press a collectionView Nothing happens and after i've pressed the UI is blocked and i cant do anything. No errors/log messages are shown. Why wont it present the viewController and why no error messages?
UITableViewController interfaceBuilder:
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
HomeProfileViewController *hpvc = [storyboard instantiateViewControllerWithIdentifier:#"NavProfile"];
hpvc.idString= [[homesDic objectAtIndex:indexPath.item] objectForKey:#"idString"];
hpvc.imageArray = [[NSMutableArray alloc]init];
hpvc.imageArray = [imageDic objectAtIndex:indexPath.item];
[self presentViewController:hpvc animated:YES completion:nil];
}
My best guess is that storyBoard is taking time to load. Maintain an instance level storyBoard object and do a lazy instantiation at didSelectItemAtIndexPath.
Lazy instantiation code:
- (UIStoryboard *)storyboard: {
if (!_storyboard) {
_storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
}
return _storyboard;
}
And in your didSelectItemAtIndexPath,
UIStoryboard *storyboard = [self storyboard];
Again, this is my best guess. It is only deduced from the given code.

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

Adding Video as a splash in storyboard ios7

I want to show a video at the beginning of the application start, just like a splash. My whole app will be in Navigation. First, i add the video in a splashViewController and set it in appDelegate as rootView and then simply set the mainViewController as rootView again.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
splashViewController = [[SplashViewController alloc] init];
splashViewController = (SplashViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"SplashViewController"];
self.window.rootViewController = splashViewController;
[NSTimer scheduledTimerWithTimeInterval:9.0 target:self selector:#selector(removeSplashScreen:) userInfo:nil repeats:NO];
return YES;
}
-(void)removeSplashScreen:(id)userInfo
{
[splashViewController removeFromParentViewController];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
mainViewController = [[MainViewController alloc] init];
mainViewController = (MainViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"MainViewController"];
self.window.rootViewController = mainViewController;
}
Now i want to start navigation from this mainViewController. I use storyBoard & add navigation from Editor > Embed in > Navigation Controller in mainViewController. Don't do it programmatically. But i know the way of implementing it using Nib.xib. Here i also put the arrow(The arrow which indicate the starting VC) mark besides of it. But it getting crash when i click a button from mainVC to go next VC. How can i set the navigation here?
If any one have the answer, please share with me. Thanks a lot in Advance.
The scenario of my storyboard:
happy to help you here in two steps
step 1-
your didFinishLaunchingWithOptions code is almost correct , but it does not matter if its working . so let focus on step two.
step 2- in -(void)removeSplashScreen:(id)userInfo
-first line is good , but you even dont need it because you are going to switch the root controller (leave it for now)
- second line (no need to write it)
- third line is really bad , what you doing here is trying to alloc you mainviewcontroller. really you need it with storyboard? no
- you should load you main view controller from storyboard
MainViewController* mainViewController = (MainViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"MainViewController"];
"this line is enough to load your main view controller from storyboard"
-next line is correct
self.window.rootViewController = mainViewController;
code seems to be working now , but still you are missing navigation controller because you have loaded mainviewcontroller from storyboard, not the navigation controller.
- two solution for it
1- load navigation controller in place of mainviewcontroller(give a storyboard id to navigation controller , load it , and make it as root controller) --i will always go with it
2- make a navigation controller with allocating it & make its rootviewcontroller the mainviewcontroller
-----------------working code ---------------
Ok Despite my advice (in the comments), here is how you can achieve this:
Display splash:
//StoryBoard
UIStoryboard *mainStoryboard = [UIStoryboard
storyboardWithName:#"Main" bundle: nil];
//Splash Controller
SplashViewController *splashViewController = [mainStoryboard
instantiateViewControllerWithIdentifier:#"SplashViewController"];
//Show it
[self.window.rootViewController presentViewController:splashViewController
animated:NO completion:nil];
Remove it:
[self dismissViewControllerAnimated:YES completion:nil];
EDIT: Still not working?
Maybe you called it to quick. do this:
viewDidLoad:
[NSTimer scheduledTimerWithTimeInterval:9.0
target:self
selector:#selector(removeSplashScreen:)
userInfo:nil repeats:NO];
function to remove:
-(void)removeSplashScreen:(id)userInfo
{
[self dismissViewControllerAnimated:YES completion:nil];
}
So the solution, according to pawan's given answer is :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
splashViewController = (SplashViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"SplashViewController"];
self.window.rootViewController = splashViewController;
[NSTimer scheduledTimerWithTimeInterval:9.0 target:self selector:#selector(removeSplashScreen:) userInfo:nil repeats:NO];
return YES;
}
-(void)removeSplashScreen:(id)userInfo
{
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
mainViewController = [[MainViewController alloc] init];
mainViewController = (MainViewController *)[mainStoryboard instantiateViewControllerWithIdentifier:#"MainViewController"];
navigationContoller = [[UINavigationController alloc] initWithRootViewController:mainViewController];
self.window.rootViewController = navigationContoller;
}
:)