Keyboard appears on top of view where no user interaction should be allowed - objective-c

I have a window-based project with two UITextFields to take input from the user.
Both are in my view controller.
I have to parse stuff in the background, so I need something that blocks user interaction in the meanwhile.
I came across this Cocoa With Love tutorial to create a loading view on top of everything. It actually works pretty well, for the most part; it works when ever the text field resigns first responder.
There is one circumstance where it doesnt work, though. When I type something in the first text field and then select the second one, the loading screen will appear, but the keyboard is on top of it, so the user could type something. I want the loading screen to be on top of the keyboard.
In the tutorial, it says to call the keyboard's superview. What is the keyboard's superview?
I tried the following different snippets in my view controller, but nothing works.
loadingView = [LoadingView loadingViewInView:[self.view.window.subviews objectAtIndex:0]];
loadingView = [LoadingView loadingViewInView:self.view];
loadingView = [LoadingView loadingViewInView:self.view.superview];
How can I make the loading screen appear on top of everything ?

The simple solution is to dismiss the keyboard when you show the loading view. I don't think you're allowed to put anything on top of the keyboard.
That said, you could try using the root window as the view to see if that has any effect.

Related

How Can I Change UIImages in a ScrollView based on a User's Input

Okay gang, I'm scratching my head on how to accomplish this one so I wanted to put it out to the world at large.
Essentially what a client wants is a way to toggle PART of a view based on whether the user selects a "Yes" or "No" option. My question is how would I go about accomplishing that?
Allow me to provide some more details. Within this specific app resides a form (a form with custom text fields, picker views, switches, sliders and other UI elements) that have all been laid out in a Storyboard, then programmed to function. About 3/4 of the way down this form, the user will be greeted with a "Yes" or "No" option and a button for each. The trick is that each view needs to have its own UI elements (text fields, sliders and buttons) appear ONLY when one of the options is selected and only BELOW the "Yes" or "No" option (all the elements above it need to remain, stay in the same place and hold the information the user enters).
An example : If the user hits the "Yes" button, below it 3 lines of text followed by a UIButton and Text Field would appear. Underneath this, other navigation buttons which navigate to other ViewControllers would appear. However, if the user hits the "No" button , all of those items I mentioned a moment ago would need to disappear, and instead a different set of text fields, labels, buttons and background image would need to appear. The navigation buttons will also need to link to other ViewControllers, different from the buttons in the "Yes" option. At this point I suggested to the client that a Navigation controller rooted at the bottom of the screen would be a good idea, but they are vehemently opposed to this and instead want the navigation options to be "dynamic" (or change according to which option is pressed, "Yes" or "No").
I have thought to attempt this programatically by simply using each button to load in a different image, this works just fine. The hitch then becomes twofold;
1) When I attempt to load the other UI elements that I get no way to lay them out in a storyboard and thus they appear in sporadic locations and
2) All this needs to be contained within a scroll view, which ALSO needs to change its size depending on how much space is needed below the yes and no option. Naturally the "Yes" section is 3 times smaller than the "No" section.
So, any ideas on how to make all this happen? I should mention at this point that the client also does not wish to simply navigate to a different ViewController, they very much want all this to occur on the same screen. I wish I had some code to share but we are still in the "design" aspect of this project and as such, very little code has been written. Any advice will be much appreciated as I've never been greeted with this type of build request before.
Create a UINavigationController object and set your view(say yourViewController) where your Yes and No buttons exist as a root view controller as below
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:yourViewController];
Now when user press YES/NO button on yourViewController view than you can programmatically create all controls text field and buttons and set frame according to your view. like
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(enter frame accordinglly)];
[self.view addSubview:txtField];
same you can add more controls like buttons or any other controls.
after that when user press any the button(on which you want another view to display) you can push other view in the navController as below
[navController pushViewController:otherViewController animated:YES];
you need to keep track of navController in your yourViewController
when you want to go back just pop view from navController
[navController popViewControllerAnimated:YES];
PS: please be watchful for memory deallocation as well.

Increasing number of living Views

I've set up a really simple project using storyboards including two views as shown here: http://i.stack.imgur.com/iRx21.png. The navigation can be done by either selecting a cell in the custom table view or hitting the back button labelled with "<<". Everything works fine except the following:
when I switch between the views, every time an instantiation happens. The profiling shows an increasing number of view objects. I would like to keep only one of each view and instantiation should be happen only once. What am I doing wrong? (I'm using ARC.)
Thanks in advance!
You should not link your back button to the parent view controller. This is what causes the new instantiation.
The way to go is to embed the table view into UINavigationController (in IB, choose Editor -> Imbed In -> Navigation Controller. Then change your segue to a Push segue. You can of course hide the navigation bar etc. to make things look exactly as you like. Then, link the back button to the controller with an IBAction and in the handler do a simple
[self.navigationController popViewControllerAnimated:YES];
This would be the appropriate logic of what you are doing. Of course, you can also push the web view modally and then handle the button click with
[self dismissModalViewControllerAnimated:YES];

UIViewControllers problems

Hi there and thank you in advice for your help. I have a really strange problem while working with ViewControllers in Xcode4. First of all I have to say that I'm not using storyboards and I prefer to create any UI element programmatically. So I've set a UIButton and I want that, when pressed, it brings me to a new view controller. This is the code I'm using for a button:
-(void)settingsAndExportHandle:(UIButton *)buttonSender {
SettingsViewController* settingView = [[SettingsViewController alloc] initWithNibName:#"SettingsViewController" bundle:nil];
settingView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:settingView animated:YES];
}
This buttons is initialized and allocated in the viewDidLoad method of the RootViewController. I want to switch to the other view controller (in this case SettingsViewController) when I press the button.
The strange thing is that when I press the button, the animation that flips the controllers goes well, but when it finishes I obtain the EXACT same things that I had on the RootViewControllers (same custom views, same buttons, same all!). The question is: what I'm missing?? I have to say that I use ARC (automatic reference counting) so I can't release or dealloc the views and buttons I've created on my RootViewController.
Any help will be appreciated. Thank you all!
Pushing and and modally presenting view controllers does not deallocate the view controller that presented them. It simply adds the additional view controller to the stack. You'll need to implement a callback method so that when the user hits the button to flip back to root view controller, your settings view controller lets the root view controller know what's about to happen so you can call a method you've written to reset the interface back to whatever state you need it at. You may also be able to use viewWillAppear: but that's a little messy.
However, according to the Apple Human Interface Guidelines, the user expects that when they push a view controller or modally present it, the view controller they were on will save state and be exactly the way they left it when they came back. It's disconcerting and annoying when state is not preserved while in a navigation controller context. It is especially annoying when it's modally presented.
Think about this - A user is in a hypothetical Mail app. They start typing out an email and set a font size and a color. They tap on the add attachment button, which brings up a modal view controller that allows them to select a picture. They select the picture and the modal view is dismissed, and in your implementation, the mail composing interface would have reset and the email content would be gone or at the very least the selected font size and color would be back to the default. That's not a good experience.

Two view controllers (with nibs) acting on the screen at the same time

Just as a disclaimer, I am an iOS beginner, in a pretty unique position. I'm an intern learning on the job, with a hard deadline for a test app, with specific specs. So I unfortunately don't have the luxury of learning all that I should about xCode and objective C.
Anyways, I am trying to create a navigation menu similar to the iPad's slide out menu. I've looked at plenty of sample code given in response to questions like this in the past. It all works perfectly fine, but I can't understand all of what they're doing. I think this results from being fairly bewildered by view controllers and delegates. And, since I'm supposed to be writing this all by myself, I can't just build off of their existing code.
My plan for this working is to have one main view controller, containing the navigation menu in a table view. This is hidden behind a normal UIView, until a button is pressed, at which point the normal UIView slides offscreen enough to reveal the menu. Upon selection of a menu item, the normal UIView would slide back to its original position, and be replaced by the relevant UIView, controlled by its view controller. In other words, clicking on the menu item relating to "Home" would load the homeViewController.xib, controlled by the homeViewController. This would be loaded in that normal UIView subview, on top of the tableView.
I'm able to load a view in that normal UIView as a result of a button press.
homeViewController *_homeViewController = [[homeViewController alloc]initWithNibName:#"homeViewController"];
[self frontView] = _homeViewController.view;
There may be some syntax errors in that code, since its from memory. I'm not able to access the computer with my code on it at the moment, but that's the general gist. Basically, that places the home view nib on the frontView (that normal UIView that's over the table view), but its not connected to homeViewController. Or at least I think that's the issue, when I press a button, that's correctly connected between the nib and the homeViewController.h, and only set to NSLog in the .m file, the application crashes.
Any idea how to connect this nib to its viewController when displayed in this way? Or, to create the nib in IB, without the associated .h and .m files, and use the main view controller as the homeViewController as well? Or, if my logic is inherently flawed, what SHOULD I do? Something with NavigationControllers?
EDIT:
I also tried a new approach- changing homeViewController's file owner to viewController, and connecting the button on homeViewController's action to viewController. This too caused a crash upon pressing the button. I really think the issue is with having multiple view controllers acting on screen at once, or having multiple views from separate nibs, controlled by one view controller on screen at once.
I suspect that your immediate problem is that _homeViewController is being freed as soon as you leave whatever method that code is in. To fix this, create a strong property in this class that holds the _homeViewController for as long as its view is needed and allocate it to that property rather than a local variable. That way, buttons (or whatever) that are part of that view still have a valid controller object backing them.
In the long run, pushing or presenting view controllers when you need the screen to change is a much better strategy but, as I said, the immediate problem.... :)

Adding and Removing UIBarButtonItems to UINavigationBar from Multiple Views

I currently have a Main Window set up as a UINavigationController (also is the root view controller), and I have two views. The first view is the login screen, and the second screen is a table view screen. What I would like to have happen is to show different UIBarButtonItems based on what screen is showing up. For instance, when the logon screen is being displayed, I want a left button on the navigation bar to be displayed (more specifically, it'd be a Settings button before logging in). Once the user logs in, I want the left button to say "Logout" and the right to be a reload button. I've tried programmatically adding the buttons, but they won't show up. Any suggestions?
Also, I've gotten most of this done in IB, but I feel like it would probably be easier to add these buttons programmatically.
All subclasses of UIViewController have a property called navigationItem. While it is readonly, you can alter its properties. So in LoginViewController, you would do self.navigationItem.leftBarButtonItem = /* Code to create the bar button */ and likewise within the TableViewController. Let me know if you need more detail.