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];
}
Related
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;
I have a screen in which a user can choose a set of meals - once the meals have been chosen the application fetches results form a database and displays a list of them. Now, I would like to implement a condition to decide whether the next screen should be loaded or not - ie. if there's no internet connection then show an alert and don't display the next screen etc.
I've implemented a system to check whether there is an internet connection or not but I'm not sure how and where to decide of the next screen should be loaded. Any ideas?
Thanks,
1.5 other options:
If you're willing to split stories and nibs, just load up a nib when you want/need to.
If you want to stick to stories exclusively, just load up another story when you need to. Same thing as loading a nib:
UIStoryboard *otherStoryboard = [UIStoryboard storyboardWithName:#"OtherStory" bundle:nil];
UIViewController *otherController = [otherStoryboard instantiateInitialViewController];
[self.navigationController pushViewController: otherController animated:YES];
Once you know in your code whether you want to display the next screen or not, can you not just add an if statement that either loads the next screen or displays a warning that there is no connection?
if (hasConnection) {
// Show next screen
} else {
// Show warning
}
You supposedly have an action that is being fired when the user selects some meals, haven't you? In this action you'd call [UINavigationController pushViewController:nextViewController animated:YES] or something like this. Put this function call into the condition of your preference, and show a popup otherwise.
I solved this issue using the answers from:
Prevent segue in prepareForSegue method? by linking the segue to my main view controller, then attaching an IBAction to the button that was originally the segue initiator and performing the logic in that method. If it all cleared then I call [self performSegueWithIdentifier:#"results" sender:self];
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
I'm making an application based on TabView Template. In the appdelegate, I've replaced the codes from applicationDidFinishLaunching method, I removed UITabView from it and replaced it with a new View. That view is actually a login screen which appears as soon as the app launches. Now, after successful login, I want the app to switch to UITabView. I tried these codes but they didn't work. :(
Nothing happens when user successfully logs in.
Here's the code I'm using:
NSArray * spanNodes = [bodyNode findChildTags:#"form"];
for (HTMLNode * spanNode in spanNodes) {
if ([[spanNode getAttributeNamed:#"action"] isEqualToString:#"foo"]){
[self.authView.view removeFromSuperview];
[window addSubview:self.tabBarController.view];
[window bringSubviewToFront:self.tabBarController.view]; //Answer to second question
}
}
You don't need to remove the tab bar controller, it's far easier to just leave it in place and push the login screen to be on top of it, covering the tabs until login is successful, as described in an answer to a related question here: Adding login screen in front of Objective C Tab Bar Application for IOS
I have a window within an iPhone application, which is displayed modally to allow the user to enter their settings for a web service upon 'first run'.
The text fields have helper text set, and when you tap them the keyboard shows and allows you to enter text.
Unfortunately the text fields do not clear the helper text, show the edit caret or show the text being entered (as in the screenshot below).
Any suggestions?
The window is being displayed with [self presentModalViewController:<controller_name> animated:YES];, which may or may not be the cause of this issue - when I run the UI via the Interface Builder 'test' application the text boxes respond like normal.
Clear when editing begins has been set for both fields.
Thanks in advance!
Edited: More information
After the info Bart Gottschalk provided I thought I should add some more information. First, the application is a Navigation Based Application.
Secondly, the test app Bart recommended worked fine, so that takes the modal window and the view out of the equation.
Third, I was presenting the modal view when the -(void)viewWillAppear... delegate method was being called - which may very well be the wrong place... however I'm not 100% sure if I should be presenting the modal view from within the didFinishLaunchingWithOptions of the App Delegate...
(this is happening on Simulator and iPhone 3.1.3)
In Interface Builder did you check the box for "Clear When Editing Begins"? With that checked the text field should clear any value once the use taps to edit which is the behavior I think you're looking for.
You can also set the same property programatically using clearsOnBeginEditing if that is convenient in your code.
My guess is that you've done this and it's not behaving as you expect. Just checking on this as a first step in helping you debug.
Also, does this happen in both the Simulator and on a testing device?
Bart
Edited Below...
This seems strange. Let's strip away everything but the basics of presenting a modal view when the application starts and see what happens.
I've recreated the most basic app (that I know of) to test presenting a modal view controller at launch and verify that field editing works fine. What happens for you when you do the same/similar in a new project?
Here is what I'm doing:
1) Create a new view-based app in Xcode called "ModalViewTest"
2) Create a new UIViewController with xib called ModalViewController
3) In ModalViewController.h add a method
-(IBAction)closeModalView;
4) In ModalViewController.m add the method implementation as
-(IBAction)closeModalView {
[self dismissModalViewControllerAnimated:YES];
}
5) In the ModalViewController.xib create two text fields and set the placeholder text for each to abcd1234 and confirm that "Clear When Editing Begins" is checked.
6) In the ModalViewController.xib add a button "Close" and set Touch Up Inside to fire "closeModalView"
7) In the application delegate (ModalViewTestAppDelegate) add the following import
#import "ModalViewController.h"
8) In the application delegate (ModalViewTestAppDelegate) applicationDidFinishLaunching add the following after the line containing [window makeKeyAndVisible];
ModalViewController *modalViewController = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
[viewController presentModalViewController:modalViewController animated:YES];
9) Save everything
10) Build and Run this new app
Does editing of the text fields work as expected? If yes, what is different about how you are building and presenting your modalView? If no, then we'll need to dig further to determine what is going on in your environment.
Second Edit Below...
When creating a navigation-based application I did the following to present the modal view at application start. Does this work for you in both your test app as well as your real app?
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
ModalViewController *modalViewController = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
[navigationController presentModalViewController:modalViewController animated:YES];
}
Well, I just figured it out, but honestly without the persistence and awesome help from Bart it would have taken much longer and been much more frustrating.
It turns out the problem was that I was using a Window instead of a View in the XIB file. This was why when showing the modal view within the Navigation controller it wouldn't display properly (i.e. only a white screen) and why the UITextField would not work properly when showing the view from the RootViewController.
So, to recap - modal views should have UIView, not UIWindow in the XIB/NIB File.
Thanks for your help Bart!
I have the same problem but in iOS7 only. I solved it by changing the tint color of textField to blue in the Storyboard