How to go to another view by clicking on the textfield? - iphone

I have a simple question, how to connect a textfield to another Control view, if I click on the textfield, instead of show the keyboard it jump to another view
Thanks for answers

In the delegate method of the TextField, wich is textFieldShouldBeginEditing, add code that go from current View to another View.
Or you can use tap gasture recognizer on TextField to get the touch.
Here is the code:
create a TextFied in your IB and connect it to .h file
#interface ViewController : UIViewController<UITextFieldDelegate>
#property (strong, nonatomic) IBOutlet UITextField *firstTF;
And in .m file add this
#synthesize firstTF;
- (void)viewDidLoad
{
[super viewDidLoad];
firstTF.delegate= self;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
secondViewController *ainfoController = [[secondViewController alloc] initWithNibName:#"secondViewController" bundle:nil];
[self presentModalViewController:ainfoController animated:YES];
return YES;
}

If you are adding the Textfield by code then,
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(x, y, width, height)];
textField.delegate = self;
[self.view addSubview:textField];
and add this method
- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField{
// you can add the code for present a new viewController here
return NO;
}
make sure you have added UITextFieldDelegate in your .h file

I really don't get in why you need this but as a developer we do firmly believe to implement all scenarios, so in your case, you can achieve the same by following the any one beneath mentioned tacts:
1) In this approach, you need to override the textfieldshouldbegin delegate and use the navigate code for moving from one screen to another and don't forget to call resignFirstResponder here.
2) While in this second approach what you can do, just overlap a custom button(with neither image nor any text) and just on his click event method write your navigation code to move another screen.
Do that stuff, you'll get what you want and in case still you find any difficulty just shout over me.

Related

Add target for UIView Programmatically

In my view I want to add a target which should be fired when I click the view. I can do the same through IB or I have done it for buttons even in code. However I have no idea how to do it for UIView programatically.
Anyone has done that before.
Help me.
For clicking a UIView you have to use UIGestureRecognizer or UITouch. This would only help in prompting an action. The UIButton has a selector method whereas the UIView does not have any such method. Also , this is same for UIImageViews etc also.
You can acheive this using UIGestureRecognizer.
Step 1:
Add your UIView as a property in your viewcontroller
#property (strong, nonatomic) IBOutlet UIView *yourView;
Step 2:
Set UIGestureRecognizer for your UIView.
- (void)viewDidLoad {
[super viewDidLoad];
UIGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleGesture:)];
[self.yourView addGestureRecognizer:gesture];
}
Step 3:
Handle the click on UIView.
- (void)handleGesture:(UIGestureRecognizer *)gestureRecognizer {
//to get the clicked location inside the view do this.
CGPoint point = [gestureRecognizer locationInView:self.yourView];
}
Remember that A UIGestureRecognizer is to be be used with a single view.

setting the value of a UILabel dynamically

i'm trying to build a quiz that sets the value of a UILabel dynamically through code.
i've done this successfully before, but for some reason it's not working this time. i suspect it's because the structure of this app is different. i've tried different fixes but haven't been able to get it to work.
the way my app is set up, i have a view controller with a view that has a segmented control. when you press one of the switches on the segmented control, it inserts a subview like this:
menuTable.hidden = YES;
additionPracticeController *additionPractice = [[additionPracticeController alloc]
initWithNibName:#"additionPractice"
bundle:nil];
self.addPracticeController = additionPractice;
[self.view insertSubview:additionPractice.view atIndex:0];
[additionPractice release];
the view controller for that subview displays its view like this:
- (void)viewWillAppear:(BOOL)animated{
firstNumberString = [NSString stringWithFormat:#"%d",arc4random() % 10];
firstNumberLabel.text = firstNumberString;
secondNumberLabel.text = secondNumberString;
[super viewWillAppear:animated]}
my outlets are connected and i can get the values to appear by setting them statically from the nib (even though that's not what i want). i've tried to set firstNumberString equal to all sorts of values, but nothing shows up when i set the values through code.
i'd really appreciate it if someone could help me solve this problem.
It sounds like you have the label connected in Interface Builder. I would need to see more code to know exactly what you are doing wrong. Make sure you are using a property for your label. The below code is a simple example of how this works.
ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UILabel *_displayMessage;
}
#property (nonatomic, retain) UILabel *displayMessage;
#end
ViewController.m
#import "ViewController.h"
#implementation ViewController
#synthesize displayMessage = _displayMessage;
- (void)viewDidLoad
{
[super viewDidLoad];
self.displayMessage.text = #"Text Changed!";
}
- (void)viewDidUnload
{
self.displayMessage = nil;
[super viewDidUnload];
}
- (void)dealloc
{
[_displayMessage release];
[super dealloc];
}
#end
Instead of making your class a subclass of UIControl just implement this method below. When the user hits done or return the keypad will resign
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
To make the text field dismiss when the user taps outside of the text field.
Place this in ViewDidLoad:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
Place this method within the class:
-(void)dismissKeyboard
{
[aTextField resignFirstResponder];
}
Also if you want to dismiss the text field from another button spefically and not just a screen tap. Then just call this from within the button.
[your_textfield_name resignFirstResponder];

How to dismiss UIKeyboardTypeNumberPad?

I'm trying to hide the number pad, but I do not want to implement a button.
Is there a way to dismiss the number pad when the user taps outside the textfield?
This is one of those questions where you read it and say "That's easy you just..". And then you go to do it and make it super complicated. And then realize it doesn't have to be that complicated.
The answer I've come up with, and I'm sure it will help someone else, Is to use an invisible UIView that never interacts but acts on other views and maybe not in the way you'd think.
The typical answer to a question about dismissing the UIKeyboardTypeNumberPad keyboard is to add a bar that has a button as the inputAccessoryView to dismiss the keyboard. If a bar and button are undesirable generally you just listen for touch events on the background and your good to go but this question is about a tableview and that makes this much harder.
But this inputAccessoryView feature is still awesome. It allows you to define a UIView or UIView subclass to be displayed when the keyboard is shown. More importantly when the keyboard is shown due to a textfield for which it is the inputAccessoryView becoming first responder.
I could yammer on but first here is some code for a lightweight class that actually performs very well in testing.
The contents of NJ_KeyboardDismisser.h are:
#import <UIKit/UIKit.h>
// For some reason neither inputView or inputAccessoryView are IBOutlets, so we cheat.
#interface UITextField (WhyDoIHaveToDoThisApple)
#property (readwrite, retain) IBOutlet UIView *inputAccessoryView;
#end
#interface NJ_KeyboardDismisser : UIView
#property (nonatomic, weak) IBOutlet UIView *mainView;
-(id)initWithMainView:(UIView *)view; // convienience method for code
#end
And the contents of NJ_KeyboardDismisser.m are:
#import "NJ_KeyboardDismisser.h"
#implementation NJ_KeyboardDismisser {
UITapGestureRecognizer *_tapGR;
}
#synthesize mainView = _mainView;
-(void)setMainView:(UIView *)view{
if (_tapGR) [_tapGR.view removeGestureRecognizer:_tapGR];
_mainView = view;
_tapGR = [[UITapGestureRecognizer alloc] initWithTarget:_mainView action:#selector(endEditing:)];
}
-(id)initWithMainView:(UIView *)view{
if ((self = [super initWithFrame:CGRectMake(0, 0, 0, 0)])){
self.mainView = view;
}
return self;
}
-(void)didMoveToWindow{ // When the accessory view presents this delegate method will be called
[super didMoveToWindow];
if (self.window){ // If there is a window one of the textfields, for which this view is inputAccessoryView, is first responder.
[self.mainView addGestureRecognizer:_tapGR];
}
else { // If there is no window the textfield is no longer first responder
[self.mainView removeGestureRecognizer:_tapGR];
}
}
#end
You may recognize the endEditing: method, as mentioned by Cosique, it is a UIView extension method that asks a views nested textfield to resign. Sound handy? It is. By calling it on the tableview the textfield it contains resigns first responder. Since this technique works on all UIViews there is no need to artificially limit this outlet to only UITableViews so the outlet is just UIView *mainView.
The final moving part here is the UITapGestureRecognizer. We don't want to add this recognizer full time for fear of screwing up the tableview's workings. So we take advantage of UIView's delegate method didMoveToWindow. We don't really do anything with the window we just check to see if we are in one; If we are then one of our textfields is first responder, if not then it's not. We add and remove our gesture recognizer accordingly.
Okay straightforward enough, but how do you use it? Well if instantiating in code you could do it like this, in tableView:cellForRowAtIndexPath::
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
UITextField *field = [[UITextField alloc] initWithFrame:CGRectMake(20, 6, 100, 31)];
[cell.contentView addSubview:field];
field.keyboardType = UIKeyboardTypeNumberPad;
field.inputAccessoryView = [[NJ_KeyboardDismisser alloc] initWithMainView:self.view];
}
If you are using static cells in a storyboard then the technique is different (obviously). First drag out a generic NSObject and place it in the dark grey strip below the view (where the other objects such as the view controller are). Then change this new object's class to be NJ_KeyboardDismisser. Then connect the "Keyboard Dismisser"'s mainView property to that view (generally a tableview). Then connect the inputAccessoryView property from any each text field in that scene you wish to the "Keyboard Dismisser".
Give it a try! The tableview acts normally. Apple's tap recognizer is smart enough to ignore the swipes on the table, so you can scroll. It also ignores touches in the textfields so you can edit and select other textfields. But tap outside a textfield and the keyboard is gone.
Note: This class's use is not limited to tableviews. If you want to use it on a regular view, just set the mainView property to be the same as the view controller's view.
The easiest way is to do this in your view controller:
[self.view endEditing: YES];
You can resign the responder inside the below function for your view:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
Make sure your view is enabled for user interaction.
when creating the text field add a tag to it.
like this Yourtextfield.tag = 1;
and in you touchesEnded method
do this :
UITextField *resignTextField = (UITextField *)[self.view viewWithTag:1];
[resignTextField resignFirstResponder];

Loading another NIB into one subview of a UIVIEW

I have a simple question that I couldn't see it answered on the whole site,
One XIB file that has a UIView as the main,
in it has another UIView that's connected to one IBoutlet so I can assign it later to load a view from another XIB file. That assignment doesn't work.. it remains blank...
#interface Subclass : UIViewController {
UIView *view1;
}
#property (nonatomic,retain) IBOutlet UIView *view1;
#end
i linked view1 to the subview in the xib file
in implementation in initWithNibName I'm loading Other XIB file and use it's view and assigning it to the view1. Still this doesn't work...
detailControler = [[XibViewControler alloc] initWithNibName:#"XibViewControler" bundle:nil];
//one aproach
[self.view1 addSubview: detailControler.view];
//another aproach
[self setView1:detailControler.view];
If I'm adding the subview programaticaly to [self.view addSubview:detailControler.view] and set a frame to it, it will go fullscreen, and hide the original view.
I also had a different approach that didn't work
How to load multiple views on each button tap when using a UISegmentedVIew
This is how I usually set up a UIView inside another view, although I'm not entirely sure if it's best practice or anything like that:
if(newViewController == nil){
newViewController = [[NewViewController alloc] initWithCoder:nil];
}
newViewController.view.autoresizingMask = UIViewAutoresizingNone;
if([newViewController.view superview] == nil){
[view1 addSubview:newViewController.view];
}
Hope that Helps!

Problem in connecting IBOutlet

In my application, I am trying to remove all existing subviews and add a new one created in Interface Builder. However, I don't seem to be able to connect the view.
When a button clicks, the following function is executed (inside a subclass of UIViewController):
// Display a list of settings to choose from
- (void) settings
{
SettingsRootController *settingsController = [[SettingsRootController alloc] initWithNibName:#"SettingsRootController" bundle:nil];
_settingsController = settingsController;
for (UIView *view in self.view.subviews)
{
[view removeFromSuperview];
}
[self.view addSubview:_settingsController.view2];
int a = [self.view.subviews count];
[self.view setNeedsDisplay];
......
}
#import <UIKit/UIKit.h>
#interface SettingsRootController : UIViewController
{
IBOutlet UIView *_view2;
}
#property(nonatomic, retain) IBOutlet UIView *view2;
Inside the Interface Builder, I created a new View-based xib. Set file owner to SettingsRootController. Randomly drag a UITextView into the xib. Connect the UITextView to view and view2 in SettingsRootController.
However, if the above line is:
[self.view addSubview:_settingsController.view2];
a would always be 0, and thus the new screen is empty.
But if change to:
[self.view addSubview:_settingsController.view];
I could see the UITextView.
When you create a view controller, its view will not be created at initialisation time. The view will be created at first access of the view property. Thats why it works when you use _settingsController.view. But when you access view2 the view will not be loaded. You could write a custom getter method like:
-(UIView*) view2 {
_view2 = self.view;
return _view2;
}
The instance var declaration is different from the property declaration! With and without underscore. Maybe thats why it doesn't work.