i used the code below to make a UITabBarController :
inside AppDelegate.h:
IBOutlet UITabBarController *rootController;
...
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
inside AppDelegate.m
#synthesize rootController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.window addSubview:rootController.view];
[self.window makeKeyAndVisible];
return YES;
}
Now i need to know how to implement this method inside AppDelegate:
- (void)SwitchToTab:(int)index{
//go to tabview 1 or 2 ...
}
You can do this:
self.rootController.selectedIndex = 2; // or whatever index you like
or this:
self.rootController.selectedViewController = oneOfTheViewControllersInTheTabController;
See the UITabBarController reference page for details.
Related
So here is my question.
I am using s subview of my MainViewcontroller to implement a TabBar in the following manner:
#class MainViewController;
#interface MyAppDelegate : NSObject <UIApplicationDelegate>
{
// UITabBarController root controller view
UITabBarController *rootController;
}
// Added
#property (nonatomic, retain) IBOutlet UITabBarController *rootController;
#end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
// Added root controller
[self.window addSubview:rootController.view];
return YES;
}
I have 4 view controllers and 4 tabs. I have specified in each tab view .m and in the main view controller:
// Autoration of view orientations
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
I get no rotation to any orientation but in my main view controller. I thought I needed to override the main view but my understanding is not sufficient. Please provide me with assistance.
Take a look at this. And at this question here.
Basically, I have two view controllers inside the MainWindow.xib that can be viewed by clicking the Bar Button in my Navigation Controller. I wanted those two view controllers to be separated from the MainWindow.xib with their own header, implementation and xib files and still make Navigation Controller inside of MainWindow.xib work in them.
To better understand it, please see the codes below:
Thanks a lot!
TestAppDelegate.h
#import <UIKit/UIKit.h>
#interface TestAppDelegate : NSObject <UIApplicationDelegate>
{
//Navigation Controller
IBOutlet UINavigationController *navigationController;
//View Controllers
UIViewController *viewController;
UIViewController *viewController2;
UIViewController *viewController3;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
//Navigation Controller
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
//View Controllers
#property (nonatomic, retain) IBOutlet UIViewController *viewController;
#property (nonatomic, retain) IBOutlet UIViewController *viewController2;
#property (nonatomic, retain) IBOutlet UIViewController *viewController3;
- (IBAction)next;
- (IBAction)next2;
#end
TestAppDelegate.m
#import "TestAppDelegate.h"
#implementation TestAppDelegate
#synthesize window = _window;
//Navigation Controller
#synthesize navigationController;
//View Controllers
#synthesize viewController;
#synthesize viewController2;
#synthesize viewController3;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//Navigation Controller
[self.window addSubview:[navigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
- (void)dealloc
{
[viewController release];
[viewController2 release];
[viewController3 release];
[navigationController release];
[_window release];
[super dealloc];
}
- (IBAction)next {
[navigationController pushViewController:viewController2 animated:YES];
}
- (IBAction)next2 {
[navigationController pushViewController:viewController3 animated:YES];
}
#end
Inside MainWindow.xib:
http://i52.tinypic.com/10xa45f.png
I normally don't touch MainWindow.xib. I suggest the following:
Create a MainController which will be your MainView that subclass UIViewController by going to File > New > New File. That will create a .h/.m and nib file for each ViewController. There add whatever UI you want for your app. For Example, add two buttons and wire those buttons to IBActions in your MainController. This should be declared and implemented in your MainController.{h/m}, respectively.
After that create another two ViewControllers, the same way.
The body of those IBActions should create an instance of the your ViewControllers and then push them.
It would look something like this:
YourViewController *yvc = [[YourViewController alloc] init];
[self.navigationController pushViewController:yvc animated:YES];
[yvc release];
Finally, you have to push the MainController in your AppDelegate and add your NavigationController to the view.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
MainViewController *mvc = [[MainViewController alloc] init];
UINavigationController *unvc = [[UINavigationController alloc] init];
[unvc pushViewController:mvc animated:NO];
[mvc release];
[self.window addSubview:unvc.view];
[self.window makeKeyAndVisible];
return YES;
}
Set the "class" property in Interface Builder (third tab in the inspector, "Custom Class") to the name of the custom class you're planning to use, and then put the name of the .xib file you want to load from in the "NIB Name" (fourth tab).
The code you're using to push the viewController is alright. Make sure to never accidentaly dealloc any of the two UIViewController.
Speaking of which, keep in mind that this approach is keeping the ViewControllers always in memory, even when not in use. Another approach is to completely remove the IBOutlets for the two ViewControllers, and do something like:
- (IBAction)next
{
MyCustomViewController *customViewController = [[MyCustomViewController alloc] initWithNibName:<#NibName or nil#> bundle:[NSBundle mainBundle]];
[navigationController pushViewController:customViewController animated:YES];
[customViewController release];
}
This creates the object when needed (If I remember right, the UI elements from the xib are cached somewhere, so that may be irrelevant[citation needed]). Just something to keep in mind, depending on the frequency of use of your two ViewControllers.
I have a little problem:
I have a view with a view controller and a tabbarcontroller in a main window.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Add the tab bar controller's current view as a subview of the window
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
On this view I have around 10 Labels.
As soon as I connect 1 Label on the Interface-Builder with the UILabel in the controller, I get a SIGBART error.
Here is my code:
#import <UIKit/UIKit.h>
#interface SelfViewController : UIViewController
{
IBOutlet UILabel *id2;
IBOutlet UILabel *username;
IBOutlet UILabel *vorname;
IBOutlet UILabel *nachname;
IBOutlet UILabel *email;
}
#end
What am I doing wrong?
Here is a solution, very basic and simple but very frustrating too:
https://discussions.apple.com/thread/1598422?threadID=1598422
UIView not shown on my window based template?
I am creating an app for this a was use the window based template and a view controller and for checking drop label but after execution it appears like clean white window.
in delegate header file i do this
#import "First_View.h"
#class First_View;
#property (nonatomic, retain) First_View *viewcontroller;
And in delegate.m
#synthesize viewcontroller=_viewcontroller;
//in implementation
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
self.window.rootViewController = self.viewcontroller; /*/**main line code here**/*/
[self.window makeKeyAndVisible];
return YES;
}
You can add the ViewController View to the window like that:
[self.window addSubView:self.viewcontroller.view];
And the view Controller should have an outlet if you are creating it using Interface Builder and don't forget to connect it:
#property (nonatomic, retain) IBOutlet First_View *viewcontroller;
I have to xibs, and I want to load which one the user wants in the settings. I'm just working on actually loading them and how would this been done.
This is the function in the delegate which I think would be where this happens.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
This is the delegate header file.
#import <UIKit/UIKit.h>
#class myAppViewController;
#interface myAppAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
myAppViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet myAppViewController *viewController;
#end
So lets say I have a xib created called modernTheme, how would I load that or the myAppViewController. If someone could do this in a generic if statement, that would be great.
This looks like your viewController is being created 'in Interface Builder'. Instead of that you could create the ViewControllers yourself and add them dynamically. Your application:didFinishLaunchingWithOptions: could look something like this:
UIViewController *viewController;
if (showModernTheme) { // from your configuration
viewController = [[YourViewControllerA alloc] initWith…];
} else {
viewController = [[YourViewControllerB alloc] initWith…];
}
// assuming YourViewControllerA + B are inheriting from UIViewController
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
Hope that helps
–f