David Biga
David Biga

Reputation: 177

Issue with setting NSString from another class - Objective C

I have a search bar that once the search button is clicked, will call another class that will set a local NSString for that class. The issue is when setting it in a function, I can call it there but if I call it from outside of that function when I move the user to that ViewController, it outputs weird things such as:

<UIKBCacheToken: 0x10c385df0>

Here what I am doing:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSString *searchType = searchBar.text;
    // SET TEXT
    search_results* myScript = [[search_results alloc] init];

    [myScript startProcess:searchType];
    [self performSegueWithIdentifier:@"moveToSearchResults" sender:self ];
}

The above is my calling method within another class.

Here is my startProcess:

- (void)startProcess:(NSString*)searchPeram {
    NSString *check = searchPeram;
    useToSearch = check;
    NSLog(@"%@",useToSearch);
}

This works great and that variable is set and outputs correctly with lets say test.

Now once the performSegueWithIdentifier is called. I try outputting the variable in the ViewdidLoad. But it would output weird things such as the above first statement. The NSString is being called as followed in the .m file:

@implementation search_results
NSString *useToSearch = @"";

- (void)viewDidLoad

Suggestions and thoughts?

Upvotes: 0

Views: 76

Answers (1)

Flexicoder
Flexicoder

Reputation: 8501

This code you are creating a new ViewController myScript ...

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    NSString *searchType = searchBar.text;
    // SET TEXT
    search_results* myScript = [[search_results alloc] init];

    [myScript startProcess:searchType];
    [self performSegueWithIdentifier:@"moveToSearchResults" sender:self ];
}

but that isn't the one that is actually being displayed, you need to implement prepareForSegue...

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:@"moveToSearchResults"]) {

        search_results * myScript = (search_results *) segue.destinationViewController;

        [myScript startProcess:searchBar.text];
     }
}

and you searchBarSearchButtonClicked changed to this

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self performSegueWithIdentifier:@"moveToSearchResults" sender:self ];
}

I would also suggest that the searchString in your search_results view controller is made into a property that you can set, rather than using startProcess

Upvotes: 2

Related Questions