I have a login page that places the username above the password then the login button last on the bottom. When I type the username, the keyboard covers the pass and login. How do I friggin close the keyboard when someone clicks outside of the text fields or is there a way to generate a close keyboard button? The only ways I have found how to do it is to programmatically add fields to the UI. Is there a way to accomplish this using the editor?
Here is my header file:
#interface Login : UIViewController{
RootViewController *rootViewController;
IBOutlet UITextField *usernameField;
IBOutlet UITextField *passwordField;
IBOutlet UIButton *loginButton;
}
#property (nonatomic, retain) UITextField *usernameField;
#property (nonatomic, retain) UITextField *passwordField;
#property (nonatomic, retain) UIButton *loginButton;
#property (nonatomic, retain) RootViewController *rootViewController;
- (IBAction) login: (id) sender;
#end
The only way I found to do it was something like:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)loadView
{
self.view = contentView;
[contentView release];
// Create a field with a Done return key
usernameField = [[[UITextField alloc] initWithFrame:CGRectMake(120.0f, 40.0f, 150.0f, 30.0f)] retain];
[usernameField setBorderStyle:UITextBorderStyleRoundedRect];
usernameField.placeholder = #"name";
usernameField.returnKeyType = UIReturnKeyDone;
usernameField.clearButtonMode = UITextFieldViewModeWhileEditing;
usernameField.delegate = self;
[contentView addSubview:usernameField];
[usernameField release];
}
Which programs the view and adds fields.Is there a way around this?
You can fix this in a number of ways. One way would be to slide the view containing your fields and button up when the keyboard becomes active by adjusting the frame top. Look at UITextFieldDelegate. If the text fields are in a table view then you can shrink the height of the table view.
If you want to hide the keyboard when a user taps outside the text field, you can add a UIGestureRecognizer to the view. When a tap is detected you can tell the active text field to resignFirstResponder.
The better approach is to listen for the UIKeyboard notifications and resize your view appropriately so the entire form is visible when the keyboard appears. From there you can wire up the return button on the keyboard to take an appropriate action depending on which text field has first responder status. For instance, pressing return from the username field will tab to the password field and pressing it from the password field would be equivalent to tapping the login button. No need to manually manage the keyboard.
Another approach is to have an inputAccessoryView for the UITextField with a "Done" button, that resigns the keyboard when clicked.
Put everything on a scroll view, and when the keyboard is firstresponder then add the height of the keyboard to the height of the scroll view. Do the inverse when the keyboard disappears.
Related
I user Interface Builder to position a UIButton and a UIImageView superimposed.
In the code, I change button label if image exists
In Example.h
#property (strong, nonatomic) IBOutlet UIButton *takePicture;
#property (strong, nonatomic) IBOutlet UIImageView *image;
In Example.m
[self.image setImage:aPhoto];
(...)
NSString *pictureButtonTitle = myCondition#"Changer la photo":#"Ajouter une photo";
(...)
[self.takePicture setTitle:pictureButtonTitle forState:UIControlStateNormal];
I use this code for a view, and I see "Change picture" correctly on my picture when myCondition is true.
But in another view, nothing appears !! WHY ?
You could either do it in the interface builder - as you did, or do it in your viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
[self.view bringSubviewToFront:self.button];
}
Arranging the outlets in the IB as you did mean that the lower object will be on top of the others... The button will be "front"/on top when it is placed below all other objects as you shown in the screenshot.
If you wish to arrange the outlets differently, you could use these methods as well:
[self.view insertSubview:self.button aboveSubview:self.imageView];
Or:
[self.view insertSubview:self.imageView belowSubview:self.button];
Well, I found out : in Interface Builder, it seems that the order you place your objects matters
If you place an UIImageView before UIButton, it works well.
I see the label
But if you do the reverse : button is under the image.
And the label is not displayed
I still don't know if it's the only way to rearrange these items ?
I'm using Xcode coding an ios7 app, whenever I try to make a text view it is editable by the user. How can I fix this so it can not be editable? It is editable in the iPhone simulator, what property do I need to change to disable this and where can I find it?
If you click on the textView in the storyboard (if you created it in the storyboard), then you can open the attributes inspector on the right and there is a checkbox that says editable. If it was not created in the storyboard, then there is a property
textView.editable = NO;
Try:
//.h
#interface YourViewController:UIViewController<UITextViewDelegate>
#property (strong, nonatomic) IBOutlet UItextView *textView;
#end
//.m
-(void) viewDidload
{
_textView.delegate =self;
}
- (BOOL)textViewDidBeginEditing:(UITextField *)textField
{
return NO;
}
If you are using storyboard, click on UITextField in storyboard and there is a property called behaviour editable: remove the tick than and build.
I'm having a hard time implementing a simple scroll on my detail view.
The app is straightforward with a Master and Detail views.
When the user taps an item on Master, the Detail view is pushed with larger photo, blog text and etc.
I would like the entire Detail view to scroll, so if the picture is tall, or the text is long, they can scroll vertically to see/read more. I do not want the user to scroll these items individually. It should feel like webpage scrolling.
Currently my Detail view loads OK but I can't make it scroll.
My DetailViewController.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController {
IBOutlet UILabel *postTextLabel; // wired to Text Label
IBOutlet UILabel *postAuthorNameLabel; // wired to Author Label
}
#property (strong, nonatomic) id detailItem;
#end
My DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
- (void)configureView;
#end
#implementation DetailViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self configureView];
}
- (void)configureView
{
if (self.detailItem) {
NSDictionary *post = self.detailItem;
NSString *postText = [post objectForKey:#"post_text"];
NSString *postAuthorName = [post objectForKey:#"post_author_name"];
postTextLabel.text = postText;
postAuthorNameLabel.text = postAuthorName;
}
}
#end
Structure on IB:
Any ideas on what's missing to make this work?
I would do the following:
1)(optional)turn your View into a scrollview by dragging it into the view in the scruture list on the side.
2)link the scrollView into your viewcontroller .h and make an Outlet connection something like this
#property (strong, nonatomic) IBOutlet UIScrollView *scroller;
(make sure you add #synthesize in the .m if you add this manually)
and make sure it is connected in IB!
3)set the contentsize of the scrollview in viewDidLoad method
scroller.contentSize = CGSizeMake(320,550);
NOTE: IBOutlet UILabel *postTextLabel; should actually probably be a UITextView so you can access ContentSize
Which would allow for the following.
CGRect frame = postTextLabel.frame;
frame.size = postTextLabel.contentSize;
postTextLabel.frame = frame;
scroller.contentSize = CGSizeMake(320, frame.size.height+200);//200 or how ever much space is above the textView
and again that only works if you use a UITextView
for ipad replace 320 with 768 or 1024, or whichever depending on orientation
The best way to connect it in IB is like this holding down control and dragging to the .h file
and make sure it is set to automatic like in the picture, and pointing to> the .h of your view.
Adding it like this also automatically adds the #synthesize for you.
Make sure UserInteractionsEnabled are checked here for our scrollview
I have a tab-bar application.
In one of the view(.xib), there is a text field and a button. The button supposed to save the text inputted in textField.
However, no matter what i typed, it always read (nil) in textField.text in debug.
When i right click the textField in .xib, i cannot drag the [New Referencing Outlet] to [File's Owner]. i can only drag the [delegate] to [File's Owner]. Does anyone have idea on why i can't do so?
Below the the .h file:
#interface AddDebtorViewController : UIViewController
{
UITextField *tDebtorName;
}
#property (retain, nonatomic) IBOutlet UITextField *tDebtorName;
-(IBAction)returnButPressed:(id)sender;
-(IBAction)addDebtorButPressed:(id)sender;
#end
Below is the .m file:
#import "AddDebtorViewController.h"
#implementation AddDebtorViewController
#synthesize tDebtorName;
- (IBAction)addDebtorButPressed:(id)sender
{
NSString *debtorName = [[NSString alloc] init];
debtorName = tDebtorName.text;
NSLog(debtorName);
}
...
#end
You must select File's Owner, press right button mouse, find your outlet and link it with the ui element.
I have a basic calculator app with a label, some numeric buttons, and some simple operatin buttons. In this app, on the bottom of the screen, the top of another view(AdvancedView) is showing and there is a button on that, and when clicked, the subview slides up to the bottom of the UILabel. I'm able to access the current text in the UILabel from the CalcViewController class because there's an outlet:
#property (strong, nonatomic) IBOutlet UILabel *display;
in the .h simply by calling:
self.display.text
But i have a squareRoot button in my AdvancedView (the view that slides up) that also needs access to the UILabel text. How can I both get and set the UILabel text from another class?
You can do this:
NSString *text = nil;
UIViewController *otherViewController;
for (UIView *view in [otherViewController.view subviews])
{
if ([view isKindOfClass:[UILabel class]])
{
UILabel *label = (UILabel *) view;
text = label.text;
break;
}
}
if (text == nil)
{
NSLog(#"Hmm, couldn't find the view...");
}
you have to #synthesize your text as well
#synthesize display
then in the file you want to use it in import the file you made the label in and then give it a pointer to that class. (in the new .h file)
CalcViewController* calcViewController;
then inside your code you would do:
calcViewController.display.text;