How to change rootViewController's view? - objective-c

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
} else {
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil] autorelease];
}
CustomView1 *CustomView = [[CustomView1 alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController.view = CustomView;
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
This appears in AppDelegate.m
I want to change rootViewcontroller's view to CustomView, and tried the code above, but the background color remained black and touch event weren't accepted.
Where is the error?

I am not sure where the fault is but I have 2 suggestions.
1) Change
CustomView1 *CustomView = [[CustomView1 alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController.view = CustomView;
to
CustomView1 *CustomView = [[CustomView1 alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
[self.viewController.view addSubview:CustomView];
or
2) In both you .xib files referenced go to the top view and change it to use a custom class. Then you can just let the app launch like normal
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
} else {
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController_iPad" bundle:nil] autorelease];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Side note:
You should avoid naming variables that begin with uppercase letters. Many coding standards have classes begin with uppercase letters and variables begin with lower case letters.

If you need two different view controllers to manage one view class, you should make the view controllers load the right view class in their loadView method or in their nib file. I guess the controllers in your code use nibs. If so, edit the nibs to set the view class to CustomView1.
CustomView1 is an unfortunate choice of class name, IMHO, because it doesn't mean anything.

Related

UiTabBarController's tabs aren't showing at all but functional, what could be causing this issue?

As you can see in the image below the tab bar is visible but the tabs aren't. However I am still able to click where both of the tabs would be and flick between 2 views.
What could be wrong?
AppDelegate Code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HypnosisViewController *hvc = [[HypnosisViewController alloc] init];
TimeViewController *tvc = [[TimeViewController alloc] init];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:hvc, tvc, nil];
[tabBarController setViewControllers:viewControllers];
[[self window] setRootViewController:tabBarController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Regards
You haven't declared any titles or images for your TabBar items.
Use something like this: initWithTabBarSystemItem:tag:

Issue pushing to detail view when tabBarController/navController in same app

I am having some difficulty getting a tableView to push to a detail view with self.navigationController and pushViewController.
I have determined that my problem comes from my appdelegate.m file. My app first loads a single window, then loads a tab bar controller. now I am trying to introduce the navigation controller on the page I need it. I am trying to merge two different projects together.
Any help would be great!
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
PDCWelcomeViewController *welcomeVC = [[PDCWelcomeViewController alloc] initWithNibName:#"PDCWelcomeViewController" bundle:nil];
self.slideUpNav = [[PDCSlideUpNavController alloc] initWithRootViewController:welcomeVC];
self.slideUpNav.navigationBarHidden = isUserLoggedIn;
self.window.rootViewController = self.slideUpNav;
[self.window makeKeyAndVisible];
return YES;
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.viewController = [[ViewController alloc] init];
[self.viewController.view setBackgroundColor:[UIColor underPageBackgroundColor]];
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navControl;
[self.window makeKeyAndVisible];
return YES;
}
It can only load PDCWelcomeViewController. After its there is a return statement. So below that statement, the code is of no worth.

Adding extra Tab Buttons to Tab Bar

I started a new Tabbed Application in XCode and I was wondering what the best way to add extra buttons to the given Tab Bar is?
Thanks!
As According to Xcode 4.3.3 When we Create Tabbed Application then it gives two ViewController. So if You want to add more you can go in AppDelegate and you can see
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil] autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
And you can see Two ViewController are added in the tabBarController and You can create new one and add init as 1 or 2 were added.
Try .......

How to ECSlidingViewController without storyboard?

I want to use ECSlidingViewController in my iOS 4.3 applications.
And I wonder how to apply this library without storyboard?
PLZ, how to? this is my code, but iOS simulator's screen is white only.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//self.window.backgroundColor = [UIColor whiteColor];
FrontViewController *frontViewController = [[FrontViewController alloc] initWithNibName:#"FrontViewController" bundle:nil];
RearViewController *rearViewController = [[RearViewController alloc] initWithNibName:#"RearViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
// create a DDMenuController setting the content as the root
//DDMenuController *menuController = [[DDMenuController alloc] initWithRootViewController:navigationController];
//menuController.leftViewController = rearViewController;
//RevealController *menuController = [[RevealController alloc] initWithFrontViewController:navigationController rearViewController:rearViewController];
ECSlidingViewController *slidingViewController = (ECSlidingViewController *)self.window.rootViewController;
slidingViewController.topViewController = navigationController;
slidingViewController.underLeftViewController = rearViewController;
self.window.rootViewController = slidingViewController;
[self.window makeKeyAndVisible];
return YES;
}
Here is your code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
FrontViewController *frontViewController = [[FrontViewController alloc] initWithNibName:#"FrontViewController" bundle:nil];
RearViewController *rearViewController = [[RearViewController alloc] initWithNibName:#"RearViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:frontViewController];
ECSlidingViewController *slidingViewController = [[ECSlidingViewController alloc] init];
slidingViewController.topViewController = navigationController;
slidingViewController.underLeftViewController = rearViewController;
self.window.rootViewController = slidingViewController;
[self.window makeKeyAndVisible];
return YES;
}
ECSlidingViewController uses iOS5, Storyboard and ARC. To put efforts into reengineering this class, I suggest you choose other classes which are ready for lower iOS versions and not using Storyboard. Some similar examples are:
https://github.com/pkluz/ZUUIRevealController
https://github.com/mystcolor/JTRevealSidebarDemo
The primary issue in your code above is that the slidingViewController isn't being instantiated.
You need this line:
ECSlidingViewController *slidingViewController = [[ECSlidingViewController alloc] init];

How to fill the screen with DetailViewController

I created a project using Master-Detail Application template but i don't need a MasterView. So i deleted Masterview files and codes but this time when i rotate the Simulator/Device in the left side of my main screen a black area stays. I want to stretch my Detail view to fill all the scren but i have no idea how to do. Can anyone please help? Thanks in advance
Note: My app needen NavigationController, firstly i tried SingleView template, but i couldn't push views on that template so i created my app in Master-Detail Application template..
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
BNT_DetailViewController *detailViewController = [[[BNT_DetailViewController alloc] initWithNibName:#"BNT_DetailViewController" bundle:nil] autorelease];
UINavigationController *detailNavigationController = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
self.splitViewController = [[[UISplitViewController alloc] init] autorelease];
self.splitViewController.delegate = detailViewController;
self.splitViewController.viewControllers = [NSArray arrayWithObjects:detailNavigationController, nil];
self.window.rootViewController = self.splitViewController;
[self.window makeKeyAndVisible];
BNT_DetailViewController *detailViewController = [[[BNT_DetailViewController alloc] initWithNibName:#"BNT_DetailViewController" bundle:nil] autorelease];
navigationController = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
This was the first appearance of the didFinishLaunchingWithOptions:of my ..AppDelegate.m but i changed it with below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
//define navigationController in ..AppDelegate.h
BNT_DetailViewController *detailViewController = [[[BNT_DetailViewController alloc] initWithNibName:#"BNT_DetailViewController" bundle:nil] autorelease];
navigationController = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
I answered my own answer in case it may be helpful for someone who lives the same pains:)