- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.main_tabs = [[UITabBarController alloc] init];
self.viewController1 = [[ViewController alloc] initWithNibName:nil bundle:NULL];
self.viewController1.title = #"Home";
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:self.viewController1];
self.viewController2 = [[ViewController2 alloc] initWithNibName:nil bundle:NULL];
self.viewController2.title = #"About";
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:self.viewController2];
self.tabs_array = [[NSArray alloc] initWithObjects:nav1,nav2, nil];
self.main_tabs.viewControllers = self.tabs_array;
[self.window addSubview:self.main_tabs.view];
return YES;
}
This is the code from my appDelegate file. I've added a UITabBarController Programmatically but it won't show up at all. There's no errors or warnings that show up and nothing is logged. Any ideas? Thanks in advance...
Change your code to this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UITabBarController *main_tabs = [[UITabBarController alloc] init];
self.viewController1 = [[ViewController alloc] initWithNibName:nil bundle:NULL];
self.viewController1.title = #"Home";
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:self.viewController1];
self.viewController2 = [[ViewController2 alloc] initWithNibName:nil bundle:NULL];
self.viewController2.title = #"About";
UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:self.viewController2];
main_tabs.viewControllers = #[ nav1, nav2 ];
self.window.rootViewController = main_tabs;
return YES;
}
Related
App crashes when I attempt to navigate to a new activity/UIViewController with the following error:
Could not cast value of type 'EXScreenOrientationViewController' (0x103d22808) to 'UINavigationController' (0x1d82964c8).
Bellow is a snippet of my AppDelegate.mm
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// FirebaseApp.configure();
if ([FIRApp defaultApp] == nil) {
[FIRApp configure];
}
RCTAppSetupPrepareApp(application);
RCTBridge *bridge = [self.reactDelegate createBridgeWithDelegate:self launchOptions:launchOptions];
#if RCT_NEW_ARCH_ENABLED
_contextContainer = std::make_shared<facebook::react::ContextContainer const>();
_reactNativeConfig = std::make_shared<facebook::react::EmptyReactNativeConfig const>();
_contextContainer->insert("ReactNativeConfig", _reactNativeConfig);
_bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:bridge contextContainer:_contextContainer];
bridge.surfacePresenter = _bridgeAdapter.surfacePresenter;
#endif
UIView *rootView = [self.reactDelegate createRootViewWithBridge:bridge moduleName:#"main" initialProperties:nil];
rootView.backgroundColor = [UIColor whiteColor];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [self.reactDelegate createRootViewController];
UINavigationController *navigationController = [[UINavigationController alloc]initWithRootViewController:rootViewController];
navigationController.navigationBarHidden = YES;
rootViewController.view = rootView;
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];
[super application:application didFinishLaunchingWithOptions:launchOptions];
return YES;
}
Basically, I have a navigation controller but I am not sure how this will work with sdk 45 following the instructions on these links:
https://github.com/expo/fyi/blob/main/expo-modules-migration.md
https://docs.expo.dev/bare/installing-expo-modules/
I am getting the following error in iOS9 only.
Here is my code:-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"login_dict"])
{
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"isLogout"] == nil || [[[NSUserDefaults standardUserDefaults] objectForKey:#"isLogout"] integerValue]== 0)
{
self.loginDict = [[BaseViewController sharedInstance] removeNullFromDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:#"login_dict"]];
self.firstViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
}
if ([[[NSUserDefaults standardUserDefaults] objectForKey:#"isLogout"] integerValue]== 1)
{
self.firstViewController = [[WelcomeViewController alloc] initWithNibName:#"WelcomeViewController" bundle:nil];
}
NSLog(#"Userinfo = %#",self.loginDict);
}
else
{
self.firstViewController = [[WelcomeViewController alloc] initWithNibName:#"WelcomeViewController" bundle:nil];
}
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.navigationController = [[BufferedNavigationController alloc] initWithRootViewController:self.firstViewController];
//[window makeKeyAndVisible];
[self.window setRootViewController:self.navigationController];
}
Note : This code is working fine in Xcode 6.4 and iOS8.
Assertion failure in -[UIApplication _runWithMainScene:transitionContext:completion:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3505.16/UIApplication.m:3294
I had to remove this line from application didFinishLaunchingWithOptions:
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
And that fixed it for me.
Using this row solved my issue (iOS 10):
[self.window setRootViewController:self.navigationController];
Was (worked for older iOS and Xcode):
[self.window addSubview:navigationController.view];
here i got the solution by checking if navigationController is nil or not:-
if (self.navigationController== nil)
{
self.navigationController = [[BufferedNavigationController alloc] initWithRootViewController:self.firstViewController];
}
else
{
[self.navigationController setViewControllers:#[self.firstViewController] animated:NO];
}
I am creating a View Controller that only appears the first time the user opens the app. I had it working by just pushing the welcome view controller, but then I couldn't get it to push the normalViewController when the user was done in the welcomeViewController. I then then tried to set the welcomeViewController as the rootViewController. This still shows the welcomeScreen, but all of the buttons on it do not work. If any one knows how to fix this or a better way to create a welcomeViewController, it would be very much appreciated. Here is the code I am using to show the rootViewController
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"TermsAccepted"]){
NSLog(#"Second time opening the app");
}
else{
WelcomeViewController *welcomeViewController = [[WelcomeViewController alloc] init];
[[UIApplication sharedApplication] keyWindow].rootViewController = welcomeViewController;
}
To create the UITabBar -
self.tabBarController = [[UITabBarController alloc] init];
[[UITabBar appearance] setTintColor:[UIColor redColor]];
// FeedViewController
feedViewController=[[FeedViewController alloc] init];
feedViewController.tabBarItem.image=[UIImage imageNamed:#"Describe Home_Icon_NormalArtboard-1"];
feedViewController.title = #"Timeline";
feedViewController.tabBarItem.title = nil;
//TodayViewController
TodayViewController *todayViewController = [[TodayViewController alloc] init];
todayViewController.tabBarItem.image = [UIImage imageNamed:#"Today_Icon"];
todayViewController.title = #"Today";
todayViewController.tabBarItem.title = nil;
//CreateViewController
self.createViewController = [[CreateViewController alloc] init];
self.createViewController.tabBarItem.image = [UIImage imageNamed:#"Create_Icon"];
self.createViewController.title = #"Create";
self.createViewController.tabBarItem.title = nil;
//AlertViewController
AlertsViewController *alertsViewController = [[AlertsViewController alloc] init];
alertsViewController.tabBarItem.image=[UIImage imageNamed:#"Alerts_IconArtboard-1"];
alertsViewController.title=#"Alerts";
alertsViewController.tabBarItem.title = nil;
//ProfileViewController
ProfileViewController *profileViewController = [[ProfileViewController alloc] init];
profileViewController.tabBarItem.image=[UIImage imageNamed:#"Profile_IconArtboard-1"];
profileViewController.title=#"Profile";
profileViewController.tabBarItem.title = nil;
NSMutableArray *tabBarViewControllers = [[NSMutableArray alloc] initWithCapacity:2];
self.tabBarController = [[UITabBarController alloc] init];
UINavigationController *feedNavigationController = [[UINavigationController alloc] initWithRootViewController:feedViewController];
[tabBarViewControllers addObject:feedNavigationController];
feedNavigationController = nil;
UINavigationController *todayNavigationController = [[UINavigationController alloc] initWithRootViewController:todayViewController];
[tabBarViewControllers addObject:todayNavigationController];
todayNavigationController = nil;
UINavigationController *createNavigationController = [[UINavigationController alloc] initWithRootViewController:self.createViewController];
[tabBarViewControllers addObject:createNavigationController];
createNavigationController = nil;
UINavigationController *alertsNavigationController = [[UINavigationController alloc] initWithRootViewController:alertsViewController];
[tabBarViewControllers addObject:alertsNavigationController];
alertsNavigationController = nil;
UINavigationController *profileNavigationController = [[UINavigationController alloc] initWithRootViewController:profileViewController];
[tabBarViewControllers addObject:profileNavigationController];
profileNavigationController = nil;
self.tabBarController.viewControllers = tabBarViewControllers;
tabBarViewControllers = nil;
[self.window addSubview:self.tabBarController.view];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"TermsAccepted"]){
NSLog(#"Second time opening the app");
rootViewController = // Your new main controller
}
else
{
rootViewController = // Terms and conditions view controller
}
self.window.rootViewController = rootViewController;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.window.rootViewController = navigationController;
return YES;
}
I have a specific view opening when opening my app from a notification. The navigation bar shows up and the navigation works correctly. The problem I'm having is getting the tab bar to show up. Here is my didFinishLaunchingWithOptions method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// This prevents the UA Library from registering with UIApplcation by default when
// registerForRemoteNotifications is called. This will allow you to prompt your
// users at a later time. This gives your app the opportunity to explain the benefits
// of push or allows users to turn it on explicitly in a settings screen.
// If you just want everyone to immediately be prompted for push, you can
// leave this line out.
// [UAPush setDefaultPushEnabledValue:NO];
//Create Airship options dictionary and add the required UIApplication launchOptions
NSMutableDictionary *takeOffOptions = [NSMutableDictionary dictionary];
[takeOffOptions setValue:launchOptions forKey:UAirshipTakeOffOptionsLaunchOptionsKey];
// Call takeOff (which creates the UAirship singleton), passing in the launch options so the
// library can properly record when the app is launched from a push notification. This call is
// required.
//
// Populate AirshipConfig.plist with your app's info from https://go.urbanairship.com
[UAirship takeOff:takeOffOptions];
// Set the icon badge to zero on startup (optional)
[[UAPush shared] resetBadge];
// Register for remote notfications with the UA Library. This call is required.
[[UAPush shared] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
// Handle any incoming incoming push notifications.
// This will invoke `handleBackgroundNotification` on your UAPushNotificationDelegate.
[[UAPush shared] handleNotification:[launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]
applicationState:application.applicationState];
// self.tabBarController = [[UITabBarController alloc] initWithNibName:#"KFBViewController" bundle:nil];
KFBViewController *rootView = [[KFBViewController alloc] initWithNibName:#"KFBViewController" bundle:nil];
KFBNavControllerViewController *navController = [[KFBNavControllerViewController alloc] initWithRootViewController:rootView];
navController.delegate = rootView;
UIViewController *aboutUs = [[AboutUs alloc] initWithNibName:#"AboutUs" bundle:nil];
KFBNavControllerViewController *navController1 = [[KFBNavControllerViewController alloc] initWithRootViewController:aboutUs];
UIViewController *contactUs = [[ContactUs alloc] initWithNibName:#"ContactUs" bundle:nil];
KFBNavControllerViewController *navController2 = [[KFBNavControllerViewController alloc] initWithRootViewController:contactUs];
UIViewController *kyfb = [[KYFB alloc] initWithNibName:#"KYFB" bundle:nil];
KFBNavControllerViewController *navController3 = [[KFBNavControllerViewController alloc] initWithRootViewController:kyfb];
// UIViewController *rsfm = [[RSFM alloc] initWithNibName:#"RSFM" bundle:nil];
// KFBNavControllerViewController *navController4 = [[KFBNavControllerViewController alloc] initWithRootViewController:rsfm];
// UIViewController *li = [[LegislatorInfo alloc] initWithNibName:#"LegislatorInfo" bundle:nil];
// KFBNavControllerViewController *navController5 = [[KFBNavControllerViewController alloc] initWithRootViewController:li];
// UIViewController *events = [[Events alloc] initWithNibName:#"Events" bundle:nil];
// KFBNavControllerViewController *navController6 = [[KFBNavControllerViewController alloc] initWithRootViewController:events];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//self.viewController = [[KFBViewController alloc] initWithNibName:#"KFBViewController" bundle:nil];
//self.window.rootViewController = self.viewController;
self.tabBarController = [[KFBTabBarViewController alloc] init];
self.tabBarController.viewControllers = #[navController, navController1, navController2, navController3];
// self.tabBarController.customizableViewControllers = nil;
self.window.rootViewController = self.tabBarController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// If application is launched due to notification,present another view controller.
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification)
{
ActionAlertsViewController *actionAlerts = [[ActionAlertsViewController alloc] initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[actionAlerts setWebViewController:wvc];
KFBNavControllerViewController *navController7 = [[KFBNavControllerViewController alloc] initWithRootViewController:actionAlerts];
[self.window.rootViewController presentViewController:navController7 animated:NO completion:nil];
}
return YES;
}
I have a UISplitViewController that is set up like so:
-(IBAction)makeStory:(id)sender{
NSLog(#"makeStory:");
makeStoryTableViewController = [[MakeStoryTableViewController alloc] initWithNibName:#"MakeStoryTableViewController" bundle:nil];
MakeSentenceTableViewController *detailViewController = [[MakeSentenceTableViewController alloc] initWithNibName:#"MakeSentenceTableViewController" bundle:nil];
UISplitViewController *splitViewController = [[[UISplitViewController alloc] init] autorelease];
UINavigationController *rootNav = [[[UINavigationController alloc] initWithRootViewController:makeStoryTableViewController]autorelease];
UINavigationController *detailNav = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
splitViewController.viewControllers = [NSArray arrayWithObjects:rootNav, detailNav, nil];
splitViewController.delegate = makeStoryTableViewController;
StoryBotAppDelegate *appDelegate = (StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window setRootViewController:splitViewController];
}
When I try to set detailView in the UISplitView delegate as a result of didSelectRowAtIndexPath:, I can only access the NavigationController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"ViewControllers: %#", [self.splitViewController viewControllers]);
MakeSentenceTableViewController *detailViewController = [[self.splitViewController viewControllers] objectAtIndex:1];
Story *storySet = [fetchedResultsController objectAtIndexPath:indexPath];
NSLog(#"detailViewController: %#", detailViewController); //Logs a UINavigationController
[detailViewController setStory:storySet]; //Fails here because Navigation Controllers Can't setStory!
[detailViewController refreshTables];
}
Which makes sense, but how do I access the TableViews that the UINavigationControllers are responsible for? If I remove *rootNav and *detailNav and replace them with the UITableViews in the splitViewController.viewControllers statement it works fine, but then I have no navigation bar:
MakeSentenceTableViewController *detailViewController = [[MakeSentenceTableViewController alloc] initWithNibName:#"MakeSentenceTableViewController" bundle:nil];
UISplitViewController *splitViewController = [[[UISplitViewController alloc] init] autorelease];
//UINavigationController *rootNav = [[[UINavigationController alloc] initWithRootViewController:makeStoryTableViewController]autorelease];
//UINavigationController *detailNav = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
splitViewController.viewControllers = [NSArray arrayWithObjects:makeStoryTableViewController, detailViewController, nil];
splitViewController.delegate = makeStoryTableViewController;
StoryBotAppDelegate *appDelegate = (StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window setRootViewController:splitViewController];
How can I access the Detail TableView Controller and keep the Navigation Bar?
Here's how I figured it out. I made two mistakes, and in those mistakes I had to use the UINavigationController.viewControllers ObjectAtIndex: property.
In the function makeStory I was incorrectly assigning a UINavigationController as a delegate instead of a makeStoryTableViewController. I fixed it by doing this, instead (pay special attention to the splitViewController.delegate section):
MakeSentenceTableViewController *detailViewController = [[MakeSentenceTableViewController alloc] initWithNibName:#"MakeSentenceTableViewController" bundle:nil];
UISplitViewController *splitViewController = [[[UISplitViewController alloc] init] autorelease];
UINavigationController *rootNav = [[[UINavigationController alloc] initWithRootViewController:makeStoryTableViewController]autorelease];
UINavigationController *detailNav = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
splitViewController.viewControllers = [NSArray arrayWithObjects:rootNav, detailNav, nil];
//splitViewController.viewControllers = [NSArray arrayWithObjects:makeStoryTableViewController, detailViewController, nil];
//RootViewController *root = (RootViewController *)[navigationController.viewControllers objectAtIndex:0]
splitViewController.delegate = [rootNav.viewControllers objectAtIndex:0];
NSLog(#"delegate: %#", [rootNav.viewControllers objectAtIndex:0]);
StoryBotAppDelegate *appDelegate = (StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(#"RootViewController: %#", appDelegate.window);
[appDelegate.window setRootViewController:splitViewController];
NSLog(#"AppDelegate.window: %#", appDelegate.window.rootViewController);
I made the same mistake again, this time in the didSelectRowAtIndexPath: and tried to setStory in a UINavigationController. To fix it, I did this (pay special attention to the bit about navControllerDetail.viewControllers:
UINavigationController *navControllerDetail;
navControllerDetail = [[self.splitViewController viewControllers] objectAtIndex:1];
MakeSentenceTableViewController *detailViewController;
detailViewController = [navControllerDetail.viewControllers objectAtIndex:0];
Story *storySet = [fetchedResultsController objectAtIndexPath:indexPath];
NSLog(#"detailViewController: %#", detailViewController);
[detailViewController setStory:storySet];
[detailViewController refreshTables];
Now if only I could figure out how to get the SplitViewController to rotate properly!