Reputation: 11830
I am beginner doing objective C.
I was trying to understand/copy Jitsi code.
In their AppDelegate.m, they have done something like this
ViewController *rootController = (ViewController *)self.window.rootViewController;
[jitsiMeet showSplashScreen:rootController.view];
return YES;
line: https://github.com/jitsi/jitsi-meet/blob/master/ios/app/src/AppDelegate.m#L59
Here showSplashScreen is this
- (void)showSplashScreen:(UIView*)rootView {
[RNSplashScreen showSplash:@"LaunchScreen" inRootView:rootView];
}
Where RNSplashScreen showSplash method is this
+ (void)showSplash:(NSString*)splashScreen inRootView:(UIView*)rootView {
if (!loadingView) {
loadingView = [[[NSBundle mainBundle] loadNibNamed:splashScreen owner:self options:nil] objectAtIndex:0];
CGRect frame = rootView.frame;
frame.origin = CGPointMake(0, 0);
loadingView.frame = frame;
}
waiting = false;
[rootView addSubview:loadingView];
}
line: https://github.com/crazycodeboy/react-native-splash-screen/blob/master/ios/RNSplashScreen.m
Now, I did the same but they used .xib file and I was using storyboard.
Like in my info.plist I have this
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
So blindly copying the code, I did something like this in my project using storyboard but I am getting following error
someMobileMobile[73326:1790521] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/xyz/Library/Developer/CoreSimulator/Devices/1B60169F-B19B-459F-85C6-E80537C7B18B/data/Containers/Bundle/Application/5CDFA673-1009-49C3-8F7D-7F1A0C8E7F57/someMobileMobile.app> (loaded)' with name 'LaunchScreen''
Is this error because I am using storyboard instead of xib?
Can someone please also explain the code snippet pasted above? and lastly, what does (ViewController *)
mean
Upvotes: 1
Views: 745
Reputation: 3368
see the difference of the following 3 ways to instantiate something from your Interface Builder work.
[NSStoryboard.mainStoryboard instantiateControllerWithIdentifier:@"youridentifier"];
NSStoryboard *launchScreen = [NSStoryboard storyboardWithName:@"LaunchScreen" bundle:NSBundle.mainBundle];
[launchScreen instantiateControllerWithIdentifier:@"yourIdentifier"];
[NSBundle.mainBundle loadNibNamed:@"SomeXibOrNib" owner:self topLevelObjects:nil];
In your example you try to load via the NSNib
method but you obviously meant the NSStoryboard
LaunchScreen.storyboard
With your code it is in fact looking for a Nib called "LaunchScreen.xib" or "LaunchScreen.nib" which the error tells you does not exist.
and your 2nd Question.
If your info.plist contains a proper entry for a MainStoryboard and this Storyboard is setup correctly, it will instantiate your Window with a ViewController as Root that was given.
Which means after the "Main" Window is loaded from that Storyboard you can access its rootViewController
property asking for its ViewController in charge.
UI/NSViewControllers come usually with a corresponding NS/UIView for free, but because subclassed Controllers may be different a typecast is needed to get rid of the warning telling you that. So to make sure the variable rootController will hold a pointer to an object of ViewController that has a property view it is typecasted.
Which works because the given RootViewController is of type "ViewController" and very likely a subclass of NS/UIViewController.
Typecasts also express you know the inheritance works because you guarantee that a view property does exist at runtime.
PS:it is always a bit confusing when chosen classnames are very simplified without any further indication which one is meant. ViewController could have been named "ANViewController" which will help you in larger projects when you have a lot different ViewControllers.
Upvotes: 3