how to use to uiviewcontroller without xib file - ios

I have one question about transition.
we use this code when we want to transition to next UIViewController:
UIViewController* secondViewController = [[UIViewController alloc] initWithNibName:#"SecondViewControllerName" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:secondViewController animated:YES];
or
when we want to add NavigationController using this code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
ViewController *viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
or
when we want add UIViewController to another UIViewController using this code:
UIViewController* secondViewController = [[UIViewController alloc] initWithNibName:#"SecondViewControllerName" bundle:[NSBundle mainBundle]];
[self.view addSubview:secondViewController.view];
now I want to know that if I don't use xib file in my project what to do???

You could use ViewController from Storyboard
ViewController *controller = (ViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
or create normally
// make Layout in viewController - viewDidLoad
ViewController *viewController = [[ViewController alloc] init];
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 64.0, 320.0, 24.0)];
label.textColor = [UIColor blackColor];
label.text = #"My text";
[self.view addSubview:label];
UIButton *button = ...;
[self.view addSubview:button];
}

Related

Switching root view controller

I've an app with UINavigationController already, but i want to switch to UITabBarController, the problem is when i switch to UItab from beginning it doesn't work, so i'm switching it in a delegate method but it doesn't work either!
all code in the app delegate.
self.navigationController = [[UINavigationController alloc] initWithRootViewController:[[UIViewController alloc] init]];
self.tabBarController = [[UITabBarController alloc] init];
if ([PFUser currentUser]) {
// Present wall straight-away
[self presentWallViewControllerAnimated:NO];
} else {
// Go to the welcome screen and have them log in or create an account.
[self presentLoginViewController];
}
[PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
The delegate method i want to switch on it:
- (void)presentWallViewControllerAnimated:(BOOL)animated {
NSLog(#"Called:presentWallViewControllerAnimated ");
// self.navigationController = nil;
self.tabBarController = [[UITabBarController alloc] init];
PAWWallViewController *wallViewController = [[PAWWallViewController alloc] initWithNibName:nil bundle:nil];
wallViewController.delegate = self;
// Set up the first View Controller
UIViewController *vc1 = [[UIViewController alloc] init];
vc1.view.backgroundColor = [UIColor orangeColor];
vc1.tabBarItem.title = #"Orange";
vc1.tabBarItem.image = [UIImage imageNamed:#"heart"];
// Set up the second View Controller
UIViewController *vc2 = [[UIViewController alloc] init];
vc2.view.backgroundColor = [UIColor purpleColor];
vc2.tabBarItem.title = #"Purple";
vc2.tabBarItem.image = [UIImage imageNamed:#"star"];
// Set up the Tab Bar Controller to have two tabs
[self.tabBarController setViewControllers:#[ vc1, vc2]];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
// [self.window addSubview:tabBarController.view];
// [self.navigationController setViewControllers:#[ tabBarController ] animated:animated];
}
Remember to handle the view transition:
UIViewController *vc = // any vc that's initialized properly
window.rootViewController = vc;
[UIView transitionWithView:window
duration:0.3 // 0.0 for immediate
options:UIViewAnimationOptionTransitionCrossDissolve // several enums to choose from here
animations:nil
completion:nil];
and you don't need makeKeyAndVisible after the first time.
You called - (void)presentWallViewControllerAnimated:(BOOL)animated but in the end of didFinishLaunchingWithOptions you called
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
So the tab bar will never works !

MMDrawerController with UITabBarController showing a black gap on top of the center view

I am opening a UITabBarController with MMDrawerController on the left side.
So from my delegate didFinishLaunchingWithOptions I am making a MMDrawerController with centerNav is a TabBarController and leftNav is a ViewController with Navigation controller. So as expected the tabbar is coming with the mmdrawer but there is a black gap on top of the LandingTabBarController.If I open the LandingTabBarController directly then it is coming from the top. Whats going wrong?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LandingTabBarController *landingTabBarController = [[LandingTabBarController alloc]initWithManagedObjectContext:self.managedObjectContext];
UIViewController * drawerNavController = [storyboard instantiateViewControllerWithIdentifier:#"DrawerViewController"];
UINavigationController *centerNav = [[UINavigationController alloc]initWithRootViewController:landingTabBarController];
UINavigationController *leftNav = [[UINavigationController alloc]initWithRootViewController:drawerNavController];
centerNav.navigationBar.barStyle = UIStatusBarStyleLightContent;
leftNav.navigationBar.barStyle = UIStatusBarStyleLightContent;
_drawerController = [[MMDrawerController alloc] initWithCenterViewController:centerNav leftDrawerViewController:leftNav];
_drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModePanningCenterView;
_drawerController.closeDrawerGestureModeMask = MMCloseDrawerGestureModePanningCenterView;
_drawerController.showsShadow = NO;
//[_drawerController setDrawerVisualStateBlock:[MMDrawerVisualState slideVisualStateBlock]];
[_drawerController setDrawerVisualStateBlock:MMDrawerSideNone];
self.window.rootViewController = _drawerController;
[self.window makeKeyAndVisible];
return YES;
}
This is my LandingTabBarController viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *tabViewControllers = [[NSMutableArray alloc] init];
UIViewController *firstViewController = [[UIViewController alloc]init];
[firstViewController.view setBackgroundColor:[UIColor redColor]];
UIViewController *secondViewController = [[UIViewController alloc]init];
[secondViewController.view setBackgroundColor:[UIColor greenColor]];
[tabViewControllers addObject:firstViewController];
[tabViewControllers addObject:secondViewController];
[self setViewControllers:tabViewControllers];
firstViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"one" image:[UIImage imageNamed:#"icon_feed"] tag:1];
secondViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"two" image:[UIImage imageNamed:#"icon_apps"] tag:2];
}
Screenshot of the debugger

IIViewDeckController is not working when push by navigation controller

Please help me in my project. I am using IIViewDeckController but its not working when pushed by view controller
Heres the hierarchy:
ViewController (Push)----> IIViewDeckController (with Left and centerview).
Here is my code when initialising the IIViewDeckController:
- (IIViewDeckController*)generateControllerStack{
self.navigationView = [[NavigationViewController alloc] initWithNibName:#"NavigationViewController" bundle:nil];
self.profileView = [[ProfileViewController alloc] initWithNibName:#"ProfileViewController" bundle:nil];
UINavigationController* navC = [[UINavigationController alloc] initWithRootViewController:self.profileView];
IIViewDeckController *deckView = [[IIViewDeckController alloc] initWithCenterViewController:navC leftViewController:self.navigationView rightViewController:nil];
deckView.leftSize = 50;
[deckView disablePanOverViewsOfClass:NSClassFromString(#"_UITableViewHeaderFooterContentView")];
return deckView;
}
then this is my code when I'm pushing IIViewDeckController using another ViewController
- (IBAction)signInAction:(id)sender {
IIViewDeckController *deckView = [[UIManager sharedItems] generateControllerStack];
[self.navigationController pushViewController:deckView animated:YES];
}
Thanks you very much guys :)
when I clicked sign in which is I will push my iiviewdeckcontroller with left and centre view this appears
Did you created it like this?
I am using it as root view controller. and it is working fine
UINavigationController* navController=(UINavigationController*)[[UIStoryboard storyboardWithName:#"Main" bundle: nil] instantiateViewControllerWithIdentifier:#"HomeViewController"];
SlideMenuViewController* slide=[[UIStoryboard storyboardWithName:#"Main" bundle:nil] instantiateViewControllerWithIdentifier:#"SlideMenuViewController"]; /
IIViewDeckController* deckController = [[IIViewDeckController alloc] initWithCenterViewController:navController
leftViewController:slide
rightViewController:nil
topViewController:nil
bottomViewController:nil];
float leftSize=self.window.frame.size.width - (88*self.window.frame.size.width)/320;
deckController.panningMode = IIViewDeckFullViewPanning;
deckController.leftSize =leftSize;
You can do like that.
//Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController"
bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc]initWithRootViewController:self.viewController];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
}
import IIViewDeckController.h in ViewController.h then
- (IBAction)GoToNext:(id)sender
{
IIViewDeckController *objIIVC = [self.storyboard instantiateViewControllerWithIdentifier:#"Your VC's Identifier"];
[self.navigationController pushViewController:objIIVC animated:YES];
}
Here is my code
- (IIViewDeckController*)generateControllerStack{
self.navigationView = [[NavigationViewController alloc] initWithNibName:#"NavigationViewController" bundle:nil];
UIViewController *centerController = [[ProfileViewController alloc] initWithNibName:#"ProfileViewController" bundle:nil];
centerController = [[UINavigationController alloc] initWithRootViewController:centerController];
self.viewDeck = [[IIViewDeckController alloc] initWithCenterViewController:centerController leftViewController:self.navigationView rightViewController:nil topViewController:nil bottomViewController:nil];
self.viewDeck.leftSize = 50;
[self.viewDeck disablePanOverViewsOfClass:NSClassFromString(#"_UITableViewHeaderFooterContentView")];
return self.viewDeck;
}

iOS - Second View is not displaying over tab view

I have a tabBarView and a GWDNativeViewController. GWDNativeViewController is a main menu. I am trying to get that view to display first and overtop the tabBarView. I have this same code set within an IBAction and it works. When I put it into the Loaddidfinishwithoptions it doesn't work.
#implementation GWDNativeAppDelegate
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
#synthesize secondView = _secondView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[GWDNativeFirstViewController alloc] initWithNibName:#"GWDNativeFirstViewController" bundle:nil];
UIViewController *viewController2 = [[GWDNativeSecondViewController alloc] initWithNibName:#"GWDNativeSecondViewController" bundle:nil];
UIViewController *viewController3 = [[GWDNativeThirdViewController alloc] initWithNibName:#"GWDNativeThirdViewController" bundle:nil];
UIViewController *viewController4 = [[GWDNativeFourthViewController alloc] initWithNibName:#"GWDNativeFourthViewController" bundle:nil];
UIViewController *viewController5 = [[GWDNativeFifthViewController alloc] initWithNibName:#"GWDNativeFifthViewController" bundle:nil];
//tabBarController Stuff
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController3, viewController4, viewController5, nil];
self.window.rootViewController = self.tabBarController;
//Specify W`enter code here`hich tab to display (Number need to be set based on button selected)
//self.tabBarController.selectedIndex = 2;
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.tabBarController presentModalViewController:secondView animated:YES];
[self.window makeKeyAndVisible];
return YES;
}
Here is the the code in the GWDNativeFirstViewController.m
The code in the IBAction works.. Code in the viewDidLoad doesn't.?
-(IBAction)pressedButton {
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalViewController:secondView animated:YES];
//[secondView release];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"First", #"First");
self.tabBarItem.image = [UIImage imageNamed:#"first"];
}
return self;
}
- (void)viewDidLoad
{
[contentWebView1 loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://m.web.org/?iapp=1#tab1"]]];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.tabBarController presentModalViewController:secondView animated:YES];
}
I'm not sure why it doesn't work in viewDidLoad, but it can work in applicationDidFinishLaunchingWithOptions: if you put your [self.window makeKeyAndVisible] line before you define secondView.
[self.window makeKeyAndVisible];
GWDNativeViewController *secondView = [[GWDNativeViewController alloc] initWithNibName:nil bundle:nil];
[secondView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self.tabBarController presentModalViewController:secondView animated:YES];
If you want it to appear immediately, change the animated option to NO, and delete the line defining the modalTransitionStyle.
The tabBarController's view hasn't loaded here yet. You could put that code into the viewDidLoad in one of your 5 view controllers.

TabBar application with NavigationBar

I'm building an App which is TabBar based. One of the tabs will display a TableView and I would like to have a NavigationBar at the top.
However I need to localize everything in this app to several languages so it needs to be done in code.
I have the tab bar set up in the AppDelegate;
#implementation AppDelegate
#synthesize window = _window;
#synthesize tabBarController = _tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[HomeViewController alloc] initWithNibName:#"HomeView" bundle:nil];
UIViewController *viewController2 = [[RecycleViewController alloc] initWithNibName:#"RecycleView" bundle:nil];
UIViewController *viewController3 = [[SettingsViewController alloc] initWithNibName:#"SettingsView" bundle:nil];
UIViewController *viewController4 = [[SettingsViewController alloc] initWithNibName:#"SettingsView" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2,viewController3, viewController4, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
Each of the windows has the code for the information on the tab.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Home", #"Home");
self.tabBarItem.image = [UIImage imageNamed:#"home"];
}
return self;
}
So far so good, but the RecyclingView (TableView) need a navigation bar where I can set the title using NSLocalizedString.
I would be really grateful for some help.
you need to add a UINavigationController at the second index of your tabBarController's viewControllers instead of adding a view controller -
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[HomeViewController alloc] initWithNibName:#"HomeView" bundle:nil];
UIViewController *viewController2 = [[RecycleViewController alloc] initWithNibName:#"RecycleView" bundle:nil];
UIViewController *viewController3 = [[SettingsViewController alloc] initWithNibName:#"SettingsView" bundle:nil];
UIViewController *viewController4 = [[SettingsViewController alloc] initWithNibName:#"SettingsView" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController2];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, navController,viewController3, viewController4, nil];
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
to set the title at navigation bar you can write this in your RecycleViewController's viewDidLoad
[self.navigationItem setTitle:#"title"];
You can localize the xibs with Interface Builder so you shouldn't have to do it programatically.
http://www.icanlocalize.com/site/tutorials/iphone-applications-localization-guide/
The answer for xcode 4.3.2 is:
alloc the UINavigationBar in RecycleViewController.m as this:
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self];
self.view addSubview:navController.view;
But there is a bug, that navigation bar will display a blank at the top, about 10pixels.
So here is another solution:
use xib builder, add a navigation bar from libary in the top of view RecycleViewController.xib, press ctrl+"navigation Item", point to RecycleViewController.h to insert an IBOutlet,then you will have a good looking navigation bar to show.

Resources