Ghobs
Ghobs

Reputation: 859

Having trouble passing contents of UITextField from one controller to another

I want to pass whatever string a user inputs into a UITextField' over to the destination view controller, but when I NSLog the value in the destination, it shows up as null. I'm also getting a warning stating:incompatible pointer types assigning to UITextField from NSString`. How can I properly make the assignment and send it over?

Originating view controller

SearchViewController.m:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    controller.itemSearch = self.itemSearch.text;
}

SearchViewController.h:

@property (weak, nonatomic) IBOutlet UITextField *itemSearch;

Destination view controller

UserCategoryChooserViewController.h:

#import <UIKit/UIKit.h>
#import "SearchViewController.h"

@interface UserCategoryChooserViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *itemSearch;

UserCategoryChooserViewController.m:

NSLog(@"The text is:'%@'", self.itemSearch.text); // results in (null)

Upvotes: 0

Views: 72

Answers (3)

Hussain Shabbir
Hussain Shabbir

Reputation: 15015

The above two answer are also correct. But if you want to make the assignment properly then modified textfield variable as NSString variable like that below:

 @interface UserCategoryChooserViewController:UIViewController
 @property (strong, nonatomic) NSString *itemSearch;

After doing this your warning incompatible pointer types assigning to UITextField from NSString will get removed

Upvotes: 1

duci9y
duci9y

Reputation: 4168

Let's go through the error you are seeing. It says that you are assigning an NSString * to a UITextField *. Unfortunately, you cannot do that. Here, itemSearch is the text field.

To make your code run properly, you need to set the text property of the itemSearch text field. The correct code would be:

controller.itemSearch.text = self.itemSearch.text;

As a side note, you should really not be meddling so deep into iOS development until you have made a strong foundation, which you have not.

Upvotes: 1

Peter Pei Guo
Peter Pei Guo

Reputation: 7870

controller.itemSearch is a UITextField *, and you tried to set it to a NSString *, that's what that warning is for. To set text of a UITextField, try this:

[controller.itemSearch setText:self.itemSearch.text];

There might be other issues, but let's start from here.

Upvotes: 1

Related Questions