0

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?

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.