How to keep a tabbar in UIViewController in iPhone? - iphone

I m very much new to iPhone development and strucked at a point .I have a UIViewController in which Im placing a tab bar and tab bar items(Search,Login) on it ..Now on startig up of the app how can I keep Search tabbaritem selected?.And If I click on login it has to redirect to another UIViewController(Login).But in UIViewController I dont need the tabbar.How can I make tab bar item to respond when login is clicked and redirect to some other view...?

From your question. It's clear that you don't need tabbar. Instead add tabbarcontroller.
Check the image I've attached . That will show you the overlay about what you should follow.
Let me know. If anything is unclear.
Enjoy programming.

On click to login,follow this code to get the application begin from LoginViewController without the tabBar..
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication]delegate];
appDelegate.window.rootViewController = LoginViewController;

Related

Show login view controller before tab bar controller

I am new to iphone development. I am developing an iphone application which contains four tabs. I have implemented it using tab bar controller. But now i need to show a login screen without tabs before tab bar controller. I have tried so many methods but didnt get the one i wanted.
Can anyone pls explain how to do this with a code snippet??
Create a new class LoginViewController. When your application launches then add the view to the window. Now when login is successful then remove it from superview and add the MainController.
Create a subclass of UITabBarController (though it's not advised by apple), but for this purposes it should be ok and do this in viewWillAppear
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
BOOL isLogged in = //do something to determine if you're logged in
if(!loggedIn){
LoginViewController *loginViewController = [[LoginViewController alloc] initWithNibName:#"LoginViewControllerNibHere" bundle:nil];
[self presentModalViewController:loginViewController animated:YES]; //or NO if you don't want it animated
[loginViewController release];
}
}
Or add this to a category for UITabBarController and import it in the app delegate or wherever you're using the UITabBarController
Create a new UIViewController subclass with a nib that represents your login screen (I'll refer to it as SignInViewController).
Open your MainWindow.nib file and add a new UIViewController
Set the new UIViewController's class type to SignInViewController
Set the UIWindow's rootViewController outlet to the new SignInViewController
Now create a new nib file and copy your existing UITabBarController to it (it's best to split nibs rather than having a single-mega nib)
Back in your MainWindow.xib change the existing UITabBarController's attributes to specify the nib name that you just created
Check this Link's source code,
it uses Login Controller as modal view with 4 tabs
http://code.google.com/p/tweetero/source/checkout
Also i tried this way ,
in my first tab view - in viewDidAppear - i'll check Login = YES then
show the LoginController
- [self.tabbarcontroller presentMOdalViewcontroller:LoginView animated:YES];
so every time u click on first tab - if u need to Login put a flag - check it & show Login View
Hope this Helps.
The best way to do this is to create a new LoginViewController as other people have mentioned and then set your rootviewController to tabBarcontroller as soon as you authenticate the user. Here is how you can do this in swift, this is snippet to put as soon you authenticate your user in LoginViewController
let tabBarController = self.storyboard?.instantiateViewControllerWithIdentifier("TabBarController") as! UITabBarController
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
appDelegate.window?.rootViewController = tabBarController
Where TabBarController is the storyboard id of your tab bar controller. It could be anything whatever name you have given it.

Show first tab on login

Upon login, I would like to program so that it shows the first tab. It does that when I first run the app but when I logout and log in again, it shows the second tab which is where the logout option is.
Is there a line of code where I could put which ensures the first tabbar item to be loaded everytime the user logs in?
Thanks in advance.
When the user logs out, or when the application closes, you should call this code:
[tabBarController setSelectedIndex:0];
This will enable you to force the UITabBarController to selected the first tab.
Update
You can get the UITabBarController instance from the UI application delegate. Eg. using the standard tab bar sample application:
MyAppDelegate *app = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
UITabBarController *tabBarController = app.tabController;
where MyAppDelegate is the name of your id<UIApplicationDelegate> class.
Try this
tabBarController.selectedIndex=1; // based on the tabbbar index
selectedIndex The index of the view
controller associated with the
currently selected tab item.
#property(nonatomic) NSUInteger
selectedIndex Discussion This property
nominally represents an index into the
array of the viewControllers property.
However, if the selected view
controller is currently the More
navigation controller, this property
contains the value NSNotFound. Setting
this property changes the selected
view controller to the one at the
designated index in the
viewControllers array. To select the
More navigation controller itself, you
must change the value of the
selectedViewController property
instead.
In versions of iOS prior to version
3.0, this property reflects the index of the selected tab bar item only.
Attempting to set this value to an
index of a view controller that is not
visible in the tab bar, but is instead
managed by the More navigation
controller, has no effect.
Availability Available in iOS 2.0 and
later. See Also #property
selectedViewController Declared In
UITabBarController.h
From UITabBarController Class Reference
Edit:
Access your app delegate and from it set the selectedIndex on your tab bar.
Edit 2:
I your appDelegate .h add
-(void)setSelectedTabBarIndex:(NSUInteger)index;
I your appDelegate .m add
-(void)setSelectedTabBarIndex:(NSUInteger)index{
tabBar.selectedIndex = index;
}
I suppose that your tabBar name is tabBar.
I your app where you want co change the tab
YourAppDelegateNane *appDelegate = [(YourAppDelegateNane *)[UIApplication sharedApplication] delegate];
[appDelegate setSelectedTabBarIndex:1];
It seems to me that when the user logs out, the application doesnt stop running.. maybe what you want to do is make sure the app "quits" when the user logs out, and then the next time the user opens the app it will be running from the start and at the first tab.. just an idea.. hope it helps

Change UITabBar Selected Tab

I have an app that I created as a Tab Bar Application, and there are 3 tabs (Home, Search, My Account). The Home view loads with some info and a search button. The search button then takes the user to the Search View, but since the user didn't select the tab the selected tab is still the Home tab. How do I change the selected tab from the Home to the Search once the Search viewDidLoad?
Thanks for the help, You guys helped me in the right direction I ended up using the App delegate to access the UITabBarController. This is what it looked like.
Motel_6AppDelegate *appDelegate = (Motel_6AppDelegate*) [[UIApplication sharedApplication] delegate];
[appDelegate.rootController setSelectedIndex:1];
Read the documentation. From within the search controller, you can call:
self.tabBarController.selectedViewController = self;
UITabBarController also has a selectedIndex property if that's more convenient for you. viewDidLoad is probably the wrong place to put this code in, though, as it might not be called every time the search controller is displayed. You should rather select the tab directly from inside the action that is called when the user taps the search button on the home screen.
Use tabBarController.selectedIndex = intIndex; // inyour case 1
use this code inside applicationDidLaunch
This snippet works for me within storyboards on iOS7 and Xcode 5.
- (IBAction)cancelButtonPressed:(id)sender
{
[self.tabBarController setSelectedIndex:1];
}

Passing a managedObjectContext through to a UITabBarController's views

I have an app which is based on the Utility template (where you flip over the view to see another). On the first view there is a login screen, then it flips over to reveal a UITabBar style interface.
I'm having trouble working out how to pass the managedObjectContext from the App Delegate (where it is created) all the way through to each of the Tab Bar's views.
App Delegate's managedObjectContext get passed to FrontLoginViewController which gets passed to BackViewTabBarViewController .. where next?
The BackViewTabBarViewController nib has a UITabBarController with a UINavigationController for each tab.
Sounds like the managedObjectContext is defined in your AppDelegate. If so, then...
From whatever viewController you want... just call
MyApplicationDelegate *appDelegate = (MyApplicationDelegate *)[[UIApplication sharedApplication] delegate];
Then use...
appDelegate.managedObjectContext
whenever you need the managedObjectContext. Change the MyApplicationDelegate to your AppDelegate and you should be good to go.
I've ran into this same problem, i'll share my solution.
First you need a reference to the Nav Controller in the Tab Bar in the nib file, make sure you connect it up.
IBOutlet UINavigationController *navigationController;
Then, get the Controller as recommended in the support docs and send it the managedObjectContext:
SavedTableViewController *saved = (SavedTableViewController *)[navigationController topViewController];
saved.managedObjectContext = self.managedObjectContext;
Alex (from another post) is right, "You should generally stay away from getting shared objects from the app delegate. It makes it behave too much like a global variable, and that has a whole mess of problems associated with it."

is it possible to reuse appdelegate in applicationDidFinishLaunching?

I am developing a tab bar application.
I have five tabs in it.
For each tab i have separate navigation controller.
For each tab's table View I want to load data from a web service.
I can do so for one tab by making a separate xmlparser class initializing it with appdelegate then call it in the applicationDidFinishLaunching .
I Can't do so for other tabs . I think that appdelegate conflicts or is it something else is the problem or any other solution .
Create a new appdelegate;
MyAppdelegate *appDelegate = (MyAppdelegate*)[[UIAplication sharedApplication] delegate];
and call applicationDidFinishLaunching.