UISearchBar textDidChange not firing - ios

I'm new to iOS programming, and I'm not sure why my textDidChange function is not firing. Searched online a bunch, but can't find out what's different between my code and everyone else's. Here's what my .h and .m files look like for this view controller:
CategoryTableViewController.h:
#import <UIKit/UIKit.h>
#import <Parse/Parse.h>
#interface CategoryTableViewController : UITableViewController
#property (weak, nonatomic) IBOutlet UIBarButtonItem *btnBack;
#property (weak, nonatomic) IBOutlet UISearchBar *txtSearchBar;
#property (strong,nonatomic) NSArray *allCategories;
#property (strong,nonatomic) NSMutableArray *filteredCategories;
//A stack containing the parent categories used to get to the current category.
#property (strong, nonatomic) NSMutableArray *parentCategories;
#end
Relevant code from CategoryTableViewController.m:
-(void)txtSearchBar:(UISearchBar*)txtSearchBar textDidChange: (NSString*)text
{
//do something
}
I used Xcode's ctrl+click+drag to create the reference to the search bar in the header file. I put breakpoints and print statements at the start of the textDidChange code, but none of it ever gets called. Any ideas?

You need to set the delegate property of txtSearchBar to use the delegate methods.
Make sure you add
self.txtSearchBar.delegate = self;
in ViewDidLoad()
and the delegate method
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
//Do something
}

If you're using a delegate method be sure to set the delegate property on the instance that needs to call the delegate so it knows there is a delegate and where to send actions.
if you're using Interface Builder and not delegate, you need to create an IBAction function, or you can use a UIControl's subclass addTarget() method to create a callback for a given event type.

Add
self.txtSearchBar.delegate = self
in viewDidLoad().

Related

textViewDidBeginEditing not working in IOS 9

i'm new in Objective C . What i'm going to do is simple just to animate keyboard up when textfield is focused and shrink when it's not. I already followed some other tutorial on how to set up this step by step but it not working.The event textViewDidBeginEditing is never get called when textfeld is focus.
#interface ViewController : UIViewController<UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *cCodeTextField;
#property (weak, nonatomic) IBOutlet UITextField *jidTextField;
- (IBAction)nextBtnTapped:(id)sender;
#end
I'm also set the delegate to the textfield but it's not working.
- (void)viewDidLoad {
[super viewDidLoad];
[self addTextFieldBorder:_jidTextField];
[self addTextFieldBorder:_cCodeTextField];
_jidTextField.delegate = self;
}
-(void) textViewDidBeginEditing:(UITextView *)textView {
NSLog(#"Enter");
}
It looks like your ViewController class is implementing methods from the wrong delegate. It is implementing UITextViewDelegate methods when it should be implementing UITextFieldDelegate methods.
Note that 'jidTextField' is of type UITextField.
Your delegate method is called 'textViewDidBeginEditing' which is a UITextViewDelegate method and takes a UITextView as a parameter.
The issue here is that your class is implementing delegate functions for UITextViewDelegate and not UITextFieldDelegate.
The correct method definition is:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
Here is a link to the documentation for the correct delegate:
https://developer.apple.com/reference/uikit/uitextfielddelegate
Hope this helps!

iOS custom UIView access properties in code

So I've created a custom UIView subclass and have it assigned to a UIView in my main storyboard. When the view loads everything is displayed properly.
The issue I'm having is that I need to be able to access properties of said custom UIView since the view is data driven.
JSON_table.h:
#import <UIKit/UIKit.h>
#interface JSON_table : UIView
#property (strong, nonatomic) IBOutlet UIView *view;
#property (weak, nonatomic) IBOutlet UISearchBar *searchbar;
#property (weak, nonatomic) IBOutlet UITableView *table_view;
#property (weak, nonatomic) NSString *data_header;
#property (weak, nonatomic) NSString *data_list;
#end
JSON_table.m:
#import "JSON_table.h"
#implementation JSON_table
- (id) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
[[NSBundle mainBundle] loadNibNamed:#"JSON_table" owner:self options:nil];
[self addSubview:self.view];
}
return self;
}
#end
(I know I'm missing delegates for tableview, ill be adding these later)
The issue I'm having is when I right click on my UIView on my storyboard I get:
The problem is when I try to connect "view" to my header file "
ViewController.h" it doesn't let me create a IBOutlet, so I cannot reference my view and its properties in code.
This is what I am trying to accomplish:
"Table" is of type UIView
Idea:
Would this have anything to do with the UIView being on the second view in my storyboard? I noticed that I don't seem to have any problem attaching to anything on the first page, but the second one I can't.
You can only connect the outlets of a view to it's class object. You are trying to connect outlets of JSON_table object to UIViewController object.
If you need to access those properties in UIViewController object. You need to import
JSON_table.h
in your view controller. And create and instantiate a object of it.
JSON_table * customView = [[JSON_table alloc]init];
Now you can access all the properties of it as:
customView.searchbar, customView.view etc.
Added by theshadow124:
Thanks to everyone who attempted to help me solve my problem. Due to being fairly new to coding for iOS I didn't realize I had to assign a custom class to every UIViewController in my storyboard(I thought they they would inherit from the base if I didn't specify). simply creating a new subclass of UIViewController and assigning it under the Identity inspector fixed my problem and now I can properly assign outlets.
Im going to accept this answer because it was one of the issues I ran into after fixing the subclass on the storyboard issue.
Please make sure that in assistant editor your are opening the same class that your custom class is contained in .

ios Passing TextView from PushView to PresentingView

I am trying to do the following, and not able to find a straightforward answer.. It is related to this :Passing uitextfield from one view to another. But not exactly.
I have a Firstview.m, from which I push to a Secondview.m. The Secondview.m has a UITextView. I allow the user to edit the UITextView on Secondview.m. Now I want to store this text value in a variable in Firstview.m. One way to to do this is as follows
in Firstview.h
#property (nonatomic) Secondview *secondView;
That is keep a secondView variable in Firstview itself. But this doesn't seem efficient. Ideally I should only have 1 NSString text field in FirstView. What is the right way to do this ? Thanks
You can achieve this by using Delegation in Objective-C.
In your SecondView.h add following right after Header Inclusion
#protocol YourDelegateName <NSObject>
-(void)setText:(NSString *)strData;
#end
Also add delegate property to your header for accessing them in calling class, like below (This goes with other properties declaration in SecondView.h file):
#property (nonatomic, weak) id<YourDelegateName> delegate;
Now, Comes the calling the delegate part. Say, you want to save the text value of UITextView of SeconView in strTextViewData of FirstView class, when the following event occurs:
- (IBAction)save:(id)sender
{
[self.delegate setText:self.txtView.text]; // Assuming txtView is name for UITextView object
}
Now, In FirstView.h add YourDelegateName in delegate list like below:
#interface FisrtView : ViewController <YourDelegateName>
#property (nonatomic, reatin) NSString *strTextViewData;
#end
And then in FisrtView.m file when you create instance of SecondView class, set delegate to self like below:
SecondView *obj = [[SecondView alloc] initWithNibName:#"SeconView" bundle:nil];
obj.delegate = self; // THIS IS THE IMPORTANT PART. DON'T MISS THIS.
Now, Implement the delegate method:
-(void)setText:(NSString *)strData
{
self.strTextViewData = strData;
}
Applying this to your code will do what you want. Also, Delegation is one of the most important feature of Objective-C language, which - by doing this - you will get to learn.
Let me know, if you face any issue with this implementation.

Call ViewController method from App Delegate

I have a method in my ViewController that opens different views based on a pin being present, it works when assigned to a button on an intial view, I can also put it in viewDidLoad method however I am using storyboard and this method viewDidLoad is firing on each view load from modal segues, so instead I am looking to call the method from my appDelegate didFinishLaunchingWithOptions.
It is basically a view redirect on app launch based on the presence of a stored pin value.
I have imported the view controller in app delegate #import "ViewController.h"
then using the current code in didFinishLaunchingWithOptions:
ViewController *rootViewController = (ViewController*)self.window.rootViewController;
[rootViewController initView];
I do this but I am getting the error
No Visible interface for 'ViewController' declares the selector 'initView'?
Here is my full ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
IBOutlet UITextField *emailField;
IBOutlet UITextField *passwordField;
IBOutlet UITextField *pinField;
IBOutlet UITextField *checkPin;
}
#property (nonatomic, strong) IBOutlet UIWebView *webView;
#property (strong, nonatomic) IBOutlet UIBarButtonItem *ShowEmailList;
-(IBAction)showList:(id)sender;
-(IBAction)checkPin:(id)sender;
-(IBAction)initView:(id)sender;
#end
and in my ViewController.m file the method:
-(IBAction)initView:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *getPin = [defaults objectForKey:#"Pin"];
if ([getPin isEqualToString:#""]){
//do something
}else{
//do something else
}
}
Any ideas on how to correctly declare my initView method in my ViewController.h file to correct this error?
Thanks
You need add an argument to this method like:
[rootViewController initView:nil];
Your -(IBAction)initView:(id)sender is expecting a parameter.
You have two ways to fix this.
Modify your method signature as -(IBAction)initView { //source code. }
Pass nil or the AppDelegate from the caller method
[rootViewController initView:nil]; or [rootViewController initView:self];

UITableView delegates not recognised after switching from UITableViewController to a UIViewController with separate UITableView

My problem is that until now I used a ViewController and inside this I create the UITableView programatically. For this ViewController I have created a class named WalkTroughViewController and set it inside Custom Class inside Identity Inspector.
Now I have changed, and I create another ViewController(deleted the old one) and drag a TableView from the Object Library. Then I have set the Custom Class of this new ViewController to WalkTroughViewController, but now when I run the project my UITableView delegates are not called.
I have also dragged a UITableViewCell inside the UITableView and set it to my custom cell class.
Any pointers what I have done wrong ?
edit:
#interface WalktroughScreenViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
- (IBAction)nextScreen;
#property (weak, nonatomic) IBOutlet UIButton *button;
#property (strong, nonatomic) IBOutlet UITableView *tableView;
#end
You can drag and assign the delegate and the dataSource of the table in the story board to the fileOwner (Your Controller)
or simply assign it programatically in ViewDidLoad method
self.tableView.delegate = self;
self.tableView.dataSource = self;
Did you assigned the delegate and the dataSource?
did you define
#interface YourClass: UIViewController <UITableViewDelegate, UITableViewDataSource>
Copy paste the code of your header so we can provide more help and see what's the issue

Resources