Issue with UISearchBar inputAccessoryView - iphone

I have created an UISearchBar and I have to insert a view in inputAccessoryView but the compiler gives me this error :
Assignment to readonly property
why this happens ? :
#property(nonatomic,retain)IBOutlet UISearchBar *searchbar;
#property (retain, nonatomic) IBOutlet UIView *keyView;
self.searchbar.inputAccessoryView = keyView;

SearchBars have a UITextView inside, you could look for it in a slightly dirty way and set the inputAccessoryView to it. I'm not sure if this will work, but it's worth a shot
UITextField *searchBarField = nil;
for (UIView *subView in searchBar.subviews) {
if ([subView conformsToProtocol:#protocol(UITextInputTraits)]) {
searchBarField = (UITextField *)subView;
searchBarField.inputAccessoryView = ...
break;
}
}

This happens, because this property is readonly, you cannot simply assign a 'UIView'. Read UISearchBar inputAccessoryView for a suggestion how to access this property. You'll have to subclass the searchbar.

Related

iPhone UITextField inputView error: Assignement to readonly property

I am having issues when I want to assign UIPicker as inputView of UITextField. It shows error " Assignement to readonly property". Please help as I think it is not related to property sythesizing.
I have added my code below:
#interface DriverWaitingDetails : UIViewController<UITextFieldDelegate,UIPickerViewDataSource, UIPickerViewDelegate>
{
IBOutlet UILabel *baseLabel;
IBOutlet UITableView *driverTableView;
IBOutlet UIPickerView *basePicker;
}
#property(nonatomic, retain) IBOutlet UIPickerView *basePicker;
#property(nonatomic,retain)IBOutlet UILabel *baseLabel;
#property(nonatomic,retain)IBOutlet UITableView *driverTableView;
#end
Implementation Code:-
-(void)viewDidLoad
{
basePicker=[[UIPickerView alloc] initWithFrame:CGRectMake(0,100,320, 500)];
self.navigationItem.title=#"Driver Table";
baseLabel.userInteractionEnabled = YES;
basePicker.delegate = self;
basePicker.dataSource = self;
[basePicker setShowsSelectionIndicator:YES];
baseLabel.inputView=nil;
[super viewDidLoad];
}
Attached Screenshot:-
You are setting the input view of UILabel.
You declared baseLabel as UILabel not UITextField.
IBOutlet UILabel *baseLabel;
You have declared
IBOutlet UILabel *baseLabel;
UILabel inherits from UIView : UIResponder : NSObject and the property is defined in UIResponder:
#property (readonly, retain) UIView *inputView
As the property inputView is read only, you cannot assign any value to it.

iphone. making UITextView but won't make it visible

#interface SomeViewController : UIViewController <UITextViewDelegate>
#property (retain, nonatomic) IBOutlet UIImageView *imageView;
#property (nonatomic, retain) UIImage *image;
#property (nonatomic, retain) IBOutlet UITextView *textView;
I need textview(editable) in my view controller.
so I set protocol ,
and also, IB to the file's owner, too. (I don't know, is it unnecessary?)
And my .m file.... viewDidLoad..
_textView = [[UITextView alloc]init];
[_textView setFrame:CGRectMake(8, 110, 304, 82)];
[_textView setFont:[UIFont boldSystemFontOfSize:12]];
_textView.editable = YES;
//[textView setText:#"hdgffgsfgsfgds"];// if uncommented, this actually visible...
[self.view insertSubview:_textView atIndex:2];
So far, it's working. But I want these methods to run...
- (void)textViewDidChange:(UITextView *)textView
{
if([_textView.text length] == 0)
{
_textView.text = #"Foobar placeholder";
_textView.textColor = [UIColor lightGrayColor];
_textView.tag = 0;
}
}
But after build, my textview is still empty.
What did I wrong?
You need to add the delegate to your textview, try with this:
_textView.delegate = self;
On the other hand,
textViewDidChange
Will only work when you change your text, if you want to place a text in the textview, you can do:
_textView.text = #"Foobar placeholder";
_textView.textColor = [UIColor lightGrayColor];
_textView.tag = 0;
In your ViewdidLoad
When synthesizing the textView, do you do it like #synthesize textView = _textView so that you can use the underscored name? Usually the underscored one is used only in the setter/getter methods (for instance when overriding them).
Note that
The text view calls this method in response to user-initiated changes to the text. This method is not called in response to programmatically initiated changes.
This is from Apple's documentation for the method you are using. This means if you are changing the text through the code, the method will not get fired.
Are you expecting [textView setText:#"hdgffgsfgsfgds"] to in turn invoke textViewDidChange:? Calling setText will not automatically invoke the textViewDidChange:.

resignfirstresponder crashes my ios app

I am new to IOS and can't seem to get this to work , I have an input filed on my app,from which I want to hide the keyboard whenever the user either presses return or the associated button (searchGo)
The following is my code :
mainViewController.h
#interface kepnMainViewController : UIViewController <kepnFlipsideViewControllerDelegate, MKMapViewDelegate>
{
MKMapView *_mapView;
IBOutlet UITextField *searchBox;
IBOutlet UIBarButtonItem *searchGo;
IBOutlet UIBarButtonItem *searchNearby;
MKAnnotationView *annotationView;
}
#property (strong, nonatomic) MKMapView *_mapView;
#property (strong, nonatomic) MapAnnotation *annotation;
#property (strong, nonatomic) UIPopoverController *flipsidePopoverController;
#property (strong, nonatomic) MKAnnotationView *annotationView;
#property (strong, nonatomic) UIBarButtonItem *searchGo;
- (IBAction)showInfo:(id)sender;
- (IBAction)searchGo:(id)sender;
- (IBAction)showNearby:(id)sender;
- (IBAction)searchBoxReturn:(id)sender;
- (void) setPlaceMarker: (CLLocationCoordinate2D) coord :(NSString*) title :(NSString*) subtitle;
#end
Appropriate .m snippet
-(IBAction)searchGo:(id)sender
{
NSLog(#"sender object %#",sender);
[sender resignFirstResponder];
NSLog(#"search button pressed and textbox = %#",searchBox.text);
}
-(IBAction)searchBoxReturn:(id)sender
{
NSLog(#"search box return ");
[sender resignFirstResponder];
}
Sorry if this is a dumb question but what am I doing wrong.??
UIBarButtonItem isnt an UIView and therefore definitely not an UIResponder. Instead, it's a subclass of NSObject, which doesn't respond to - (void)resignFirstResponder.
(solution: remove the [sender resignFirstResponder]; lines)
So if your view is manually programmed than you might add the following check to your code:
if ( [sender isKindOf: [UIResponder class]] == YES && [(UIResponder*)sender canResignFirstResponder] == YES )
[sender resignFirstResponder];
If you want to hide keybord on button tap assuming your searchBox is current responder than you should write
[searchBox resignFirstResponder]; // This will close keyboard

UITextView in tableHeaderView can't find delegate

I have a UITableView tableHeaderView defined in a nib file. The tableHeaderView contains a UITextView. The UITextView's delegate is set to the UITableViewController and the UITableViewController supports the UITextViewDelegate protocol. The UITableViewController is setting various properties in the tableHeaderView including the UITextView's text and that all works fine. But textFieldShouldReturn:(UITextField *)textField in the UITableViewController never gets called and I can't get the keyboard to close after I've tapped the UITextView.
I've tried every suggestion I can find in the docs and various posts, but no luck. None of the examples I've found have this specific case of a UITextView inside a tableHeaderView.
Is there something special I need to do. Has anyone done this successfully?
I've tried setting the UITextView delegate in code, but no luck.
#class Decision;
#interface DecisionDetailViewController : UITableViewController <UINavigationControllerDelegate, UITextViewDelegate> {
Decision *decision;
UIView *tableHeaderView;
UITextView *nameTextField;
}
#property (nonatomic, retain) Decision *decision;
#property (nonatomic, retain) IBOutlet UIView *tableHeaderView;
#property (nonatomic, retain) IBOutlet UITextView *nameTextField;
#end
#implementation DecisionDetailViewController
#synthesize decision;
#synthesize tableHeaderView;
#synthesize nameTextField;
- (void)viewDidLoad {
self.navigationItem.rightBarButtonItem = self.editButtonItem;
if (tableHeaderView == nil) {
[[NSBundle mainBundle] loadNibNamed:#"DecisionDetailHeader" owner:self options:nil];
self.tableView.tableHeaderView = tableHeaderView;
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[nameTextField resignFirstResponder];
return YES;
}
textFieldShouldReturn is a method of UITextFieldDelegate and not UITextViewDelegate. Thats probably your problem.

add something above of iPhone / iPad keyboard?

i was wondering how can i add a view on ios Keyboard ? like this :
you will want to look into the inputAccessoryView property in the UITextView or UITextField class (depending on what you are using) for a custom view above the keyboard.
If you want to only show the accessory view by itself then subclass your UIResponder view that presents the keyboard and redeclare inputView as read/write and return your accessory view in that method.
Interface
#property (atomic, retain) UIView *inputView
Implementaion
- (UIView *)inputView { return accessoryView; }
- (void)setInputView:(UIView *)aView {
if (accessoryView != aView) {
[accessoryView release];
accessoryView = [aView retain];
}
}
More information on redeclaring and adding both inputView and inputAccessoryView for any UIResponder subclass in the official Apple documentation: https://developer.apple.com/library/ios/documentation/StringsTextFonts/Conceptual/TextAndWebiPhoneOS/InputViews/InputViews.html