UITextView.text doesn't work - iphone

hi i'm studying iOS programming.
i have a trouble to use UITextView.
i made a project and open viewController.xib
i dragged in UISearchBar, UITextView and UILabel
and my viewController is followed UISearchBarDelegate
i want to change textView.text's contents but it doesn't.
here's my code.
-(void)viewDidLoad
{
[super viewDidLoad];
self.mySearchbar.delegate = self;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
self.myLabel.text = #"hoho";
self.myTextview.text = #"hoho";
}
when i clicked search bar and typing any character and return,
myLabel's text is set to hoho.
but myTextview.text doesn't set. no effect.
why is that?? i'm confused.. please help me

Did you link up the Outlet in Interface Builder? In IB, click File's Owner, select Outlets on the right pane, link myTextview to the UITextview you put on the view?
Also, since you are calling "self" I assume you created a property for myTextview as an IBOutlet in your .h file?

Are you setting(connecting) the delegate of UITextView as well. That might be the problem.

Try to set the TextView Color to black
[YourTextview setTextColor:[UIColor blackColor]];
Sometimes this helps.

As I recall, instead you should be using
[self.myLabel setText:#"hoho"];

Related

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

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.

UIToolbar as Subview into another UIViewController is not clickable

I am creating an app with multiple UIViewController, they have common menu and toolbar in header. i have created a uiviewcontroller with nib for top UIToolBar with some UIToolBarButtons. I am calling in another UIViewController, my problem is this that UIToolBar is displaying but any of UIToolbar button is not clickable now, they are not working. so it only showing the view which is not working at all.
Please see UIView UserInterfaceEnabled or not. if not then just write below code in your viewdidload
self.view.userInteractionEnabled = YES;
OR also try this
[self.view bringSubviewToFront:youtoolbar];
just try this code something skip you from your code
[yourBarButton setTarget:self];
[yourBarButton setAction:#selector(yourBtnClicked:)];
[yourToolBar setUserInteractionEnabled:YES];
[self.view setUserInteractionEnabled:YES];
[self.view bringSubviewToFront:yourToolBar];
UPDATE:
For use this UIToolBar to every UIViewController, just add it in window and set method in AppDelegate class also...
hope this help you...
Try this.. You remove your toolbar in viewWillAppear and add it again like
(void)viewWillAppear:(BOOL)animated
{
[yourtoolbar_reference removeFromSuperview];
[self.view addSubview:yourtoolbar_reference];
}

how to solve Use of undeclared identifier 'UIKeyboard' error?

Am trying to show UIKeyboard without using UITextView and UITextField. Am following the below source code to show the Keyboard in my app.
UIKeyboard *keyboard = [[[UIKeyboard alloc] initWithFrame: CGRectMake(0.0f, 220.0f, 320.0f, 216.0f)] autorelease];
[keyboard setReturnKeyEnabled:NO];
[keyboard setTapDelegate:editingTextView];
but the code showing below errors,
Use of undeclared identifier 'UIKeyboard'
Use of undeclared identifier 'keyboard'
Anyone please help to solve this error? and please tell me this is the right way to show keyboard without using UITextView and UITextField?
You can use UIKeyInput delegate.
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController<UIKeyInput>
And in your ViewController.m class. return YES; to canBecomeFirstResponder:
-(BOOL)canBecomeFirstResponder
{
return YES;
}
It will show you keyboard. But Where you write if there is no UITextView or UITextField.
Well for writing on UIView see this logics - UIKeyInput- Displaying keyboard in iPhone without UITextField or UITextView

UISearchBar becomeFirstResponder returns 0, but have valid reference to UISearchBar

I have a UIViewController with UISearchBar (and SearchDisplayController) along with a UITableView. When navigating to this view controller, I want to auto-focus on the UISearchBar (bring up the keyboard with focus on the text field in the search bar). Everything says to use
[searchBar becomeFirstResponder]
(assuming searchBar is an outlet to the UISearchBar)
I put this at the end of viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
BOOL did = [searchBar becomeFirstResponder];
[searchBar setText:#"donkey"];
}
Variable did is 0 (and focus doesn't happen), but the search bar's text is successfully changed to donkey.
What am I doing wrong?
I'm using iOS 5 with ARC and latest Xcode (4.3.2).
Got it working. Just had to put it in viewDidAppear instead of viewDidLoad or viewWillAppear.
add your code to -(void)viewDidAppear;
This worked for me:
[window makeKeyAndVisible]
we must ensure the METHOD becomeFirstResponder be performed on mainThread
so make it like :
[xxx performSelectorOnMainThread:#selector(becomeFirstResponder) withObject:nil waitUntilDone:NO];

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];