Passing a string to another view - objective-c

I have a UITextField in my first view that accepts search criteria however my search box is in my second view. The idea is to pass the textfield data that the user enters into the search box that filters a table view in the second view controller. I have tried setting the secondviewcontroller.searchText = self.search.text however it doesn't seem to be updating the search box in the second view controller with the new data. My code is as follows:
First View.m
- (IBAction)search:(id)sender
{
PlaceList *placelist = [[PlaceList alloc] initWithNibName:#"PlaceList" bundle:nil];
NSLog(#"%#", search.text);
placelist.searchBar.text = search.text;
[self.navigationController pushViewController:placelist animated:YES];
[placelist release];
}
PlaceList.m
#pragma mark UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// only show the status bar’s cancel button while in edit mode
useSearchData = YES;
self.searchBar.showsCancelButton = YES;
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
// flush the previous search content
[tableData removeAllObjects];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
self.searchBar.showsCancelButton = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if([searchText isEqualToString:#""] && [__searchBar.text isEqualToString:#""]){ //if nothing is in the search bar show normal table
useSearchData = NO;
[self.tableView reloadData];
[self.searchBar resignFirstResponder];
return;
}
else
{
searchText = __searchBar.text;
useSearchData=YES;
NSPredicate * p = [NSPredicate predicateWithFormat:#"name contains[cd] %#",searchText]; //comparing stored locations to searchText
self.searchResults = [CoreDataBasicService fetchResultsForEnity:#"Place" WithPredicate:p andSortDiscriptor:#"name" Ascending:YES];
[self.tableView reloadData];
}
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
useSearchData = NO;
[self.searchResults removeAllObjects];
[self.tableView reloadData];
[self.searchBar resignFirstResponder];
self.searchBar.text = #"";
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)_searchBar
{
[searchBar resignFirstResponder];
}
When the search IBAction is pressed I want to push to the placeList view and update the search with the text the user entered in the textfield on the first view controller.
Thanks in advance.

Try initializing PlaceList(VC) with the search Text, saving it to an instance variable. Then, in your PlaceList(VC)'s ViewDidLoad Method, set the text there

Related

Objective C Objects Only Being Changed In Method Scope

I am making a chat application, and when the person logs onto the chat successfully, the server replies with the people that are online at that moment. That string (the server sends a string) gets converted to a NSMutableArray, which is then stored into a NSMutableArray called tableData, which is the data source for a NSTableView. When the online people are stored into tableData, the NSLog output shows that tableData is filled. However, when that method is done, and the login view is closed, in the debugger tableData says 0 Objects, and the NSTableView doesn't fill, which it normally does. Here is are my methods (one calls another):
- (void)getPeople:(NSString *)outputMessage
{
NSArray *newTableData = [outputMessage componentsSeparatedByString:#";"];
self.tableData = [NSMutableArray arrayWithArray:newTableData];
//[self.tableData removeObjectAtIndex:0];;
NSLog(#"Data: %#", self.tableData);
[self.people reloadData];
}
- (void)accessGrantedWithOnlineUsers:(NSString *)users
{
NSLog(#"Access Granted");
self.isLoggedIn = YES;
[self.loginButton setTitle:#"Logout"];
[self getPeople:users];
}
Here is my method that opens and closes the login view:
- (IBAction)loginToChat:(id)sender {
NSLog(#"Called");
if (self.loginPopover == nil) {
NSLog(#"Login Popover is nil");
self.loginPopover = [[NSPopover alloc] init];
self.loginPopover.contentViewController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
}
if (!self.loginPopover.isShown) {
NSLog(#"Login Popover is opening");
[self.loginButton setTitle:#"Cancel"];
[self.settingsButton setEnabled:NO];
[self.send setEnabled:NO];
[self.message setEnabled:NO];
[self.loginPopover showRelativeToRect:self.loginButton.frame ofView:self.view preferredEdge:NSMinYEdge];
}
else {
NSLog(#"Login Popover is closing");
if (self.isLoggedIn) {
[self.loginButton setTitle:#"Logout"];
}
else {
[self.loginButton setTitle:#"Login"];
}
[self.settingsButton setEnabled:YES];
[self.send setEnabled:YES];
[self.message setEnabled:YES];
[self.loginPopover close];
}
}
Any help would be greatly appreciated because I have a deadline for this project.
Instead of
self.tableData = [NSMutableArray arrayWithArray:newTableData];
can you try
self.tableData = [newTableData copy];
The only thing i can think of is - is the tableData a strong property?
After this
self.tableData = [NSMutableArray arrayWithArray:newTableData];
write this snippet:
[self setTableData:tableData]

How do I add new table cell data from one modal view controller?

Here's the process I have currently:
1) Tap Add Button
2) Fill out UITextFields in my modal view controller
3) Tap Save Button
4) Data is added to a UITableViewCell
5) Repeat steps 1 - 3
6) New data is added to second cell, but the data in both cells is from the new input
How do I use the same modal view controller to add new data yet retain it in my previous cell(s) and even view the data when I tap on those cell(s)?
Here is my process for going to the modal view controller and adding data to it:
-(IBAction)goToModalView:(id)sender
{
if (self.educationViewController == nil)
{
EducationViewController * temp = [[EducationViewController alloc] initWithNibName:#"EducationViewController" bundle:[NSBundle mainBundle]];
self.educationViewController = temp;
_dataArray = [[NSMutableArray alloc] init];
[_dataArray addObject:temp];
}
else
{
[self addEducation:sender];
}
[self presentViewController:self.educationViewController animated:YES completion:nil];
}
-(IBAction)addEducation:(id)sender
{
[_myTableView beginUpdates];
[_dataArray addObject:self.educationViewController.majorString];
NSArray * paths = [NSArray arrayWithObject:[NSIndexPath indexPathForRow:[_dataArray count]-1 inSection:0]];
[_myTableView insertRowsAtIndexPaths:paths withRowAnimation:UITableViewRowAnimationTop];
[_myTableView endUpdates];
}
Here is where I am saving the text in my modal view controller:
-(IBAction)saveData
{
if (_majorTextField.text == nil)
{
_majorString = #"";
}
else
{
_majorString = [[NSString alloc] initWithFormat:#"%#", _majorTextField.text];
[_majorTextField setText:_majorString];
NSUserDefaults * majorDefault = [NSUserDefaults standardUserDefaults];
[majorDefault setObject:_majorString forKey:#"major"];
}
// Other text field code here
[self dismissViewControllerAnimated:YES completion:nil];
}
Any advice is appreciated! Much thanks!
Instead of adding the data directly to the UITableView, I would add it to an NSMutableArray. I would then use that array as the datasource for the table, and after the save button is pressed, I would call
[_myTableView reloadData];
I don't understand what in goToModalView() why you do "[_dataArray addObject:temp];". But leave it along, in addEducation(), you may use
[_myTableView reloadData];
to read the whole table.

How to reload UITableView after the UISearchBar ends editing

I created a UISearchBar. It is working but not the way I want. If I enter any first letter on the UISearchbar and then I click on the SearchButton, it doesn't work but when I push the next controller and I come back, then I see my search result in TableView. The first time my TableView does not refresh.
Here is my custom cell class and my controller class
#synthesize myTableView;
#synthesize tabledata;
#pragma mark -
#pragma mark Initialization
#pragma mark -
#pragma mark View lifecycle
-(void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
app = (JourneyAppDelegate *)[[UIApplication sharedApplication]delegate];
sBar =[[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
sBar.delegate=self;
[self.view addSubview:sBar];
searcheddata =[[NSMutableArray alloc]init];
NSLog(#"*************:%&",list);
list=[[NSMutableArray alloc]init];
tabledata =[[NSMutableArray alloc]init];
list = [app.journeyList retain];
[tabledata addObjectsFromArray:list];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100.0;
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tabledata count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"*************:%&",list);
static NSString *CellIdentifier = #"Cell";
TJourneyListCell *cell =(TJourneyListCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[TJourneyListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
NSLog(#"%#",cell);
}
NewJourney *newjoruneobject = [tabledata objectAtIndex:indexPath.row];
cell.namelbl.text = newjoruneobject.journeyname;
cell.distancelbl.text = newjoruneobject.journeylocation;
cell.infolbl.text = newjoruneobject.journeydescription;
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
return cell;
}
#pragma mark UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
// only show the status bar’s cancel button while in edit mode
[tabledata removeAllObjects];
sBar.showsCancelButton = YES;
[searchBar setShowsCancelButton:YES animated:YES];
sBar.autocorrectionType = UITextAutocorrectionTypeNo;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = NO;
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[tabledata removeAllObjects];// remove all data that belongs to previous search
if([sBar.text length] != 0)//|| searchText==nil)
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"journeyname contains[cd] %#", searchBar.text];
[tabledata addObjectsFromArray:[list filteredArrayUsingPredicate: predicate]];
[myTableView reloadData];
return;
}
NSLog(#"Counter:-'%d'",[tabledata count]);
[myTableView reloadData];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{ // if a valid search was entered but the user wanted to cancel, bring back the main list content
[tabledata removeAllObjects];
[tabledata addObjectsFromArray:list];
#try
{
[myTableView reloadData];
}
#catch(NSException *e)
{
}
[sBar resignFirstResponder];
sBar.text = #" ";
}
// called when Search (in our case “Done”) button pressed
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
myTableView.allowsSelection = YES;
myTableView.scrollEnabled = YES;
[myTableView reloadData];
}
- (void)dealloc {
[super dealloc];
[mLable1 release];
[myTableView release], myTableView = nil;
[tabledata dealloc];
}
#end
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
searchBar.showsCancelButton = NO;
[myTableView reloadData];
}
I was having difficulties with this as well until I got to this post. I looked through your functions and decided to throw the reload data in the function above it works fine now! Hope this helps!
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[tableView reloadData];
}
You can use this delegate method to reload table, after search table disappears:
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
Swift version
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchActive = false //Local variable used to manipulate your cells
self.newsTableView.reloadData()
}

Resign first responder from separate class

I have a class that makes a keyboard toolbar which has "Next", "Previous", and "Done" buttons on it. Is there a way for this class to know (or find out) what objects are on the screen at any time?
E.g., can it see what the current view is and what the text fields on it are, and then be able to resign the first responder?
If you specifically want to resign first responder without the need to known which view is the first responder you can send resignFirstResponder to "nil" like this:
[[UIApplication sharedApplication] sendAction:#selector(resignFirstResponder) to:nil from:nil forEvent:nil];
This is documented behaviour although I cannot find in the docs right now.
Is there a way for this class to know
(or find out) what objects are on the
screen at the time?
Find the momma view and you can iterate through all the objects on the screen (because they will be UIViews too) like this. Note that you may need to add recursion:
for (UIView *view in mommaView.subviews) {
do something to the view
}
You can start at the Window class and go down from there, asking [view respondsTo:#selector(isFirstResponder) && [view isFirstResponder] on each. Some debugging code that I use might come in handy as a template and also while debugging:
+ (void) dumpWindowFrom:(NSString *) fromText {
[self dumpViews:[[UIApplication sharedApplication] keyWindow] from:fromText];
}
void dumpViewsRecursive(UIView* view, NSString *text, NSString *indent)
{
Class cl = [view class];
NSString *classDescription = [cl description];
// while ([cl superclass]) //restore to print superclass list
// {
// cl = [cl superclass];
// classDescription = [classDescription stringByAppendingFormat:#":%#", [cl description]];
// }
if ([text compare:#""] == NSOrderedSame)
NSLog(#"%d: %# %# %#", (int)view, classDescription, NSStringFromCGRect(view.frame), view.hidden ? #"Inv" : #"Vis");
else
NSLog(#"%d: %# %# %# %#", (int)view, text, classDescription, NSStringFromCGRect(view.frame), view.hidden ? #"Inv" : #"Vis");
for (NSUInteger i = 0; i < [view.subviews count]; i++)
{
UIView *subView = [view.subviews objectAtIndex:i];
NSString *newIndent = [[NSString alloc] initWithFormat:#" %#", indent];
NSString *msg = [[NSString alloc] initWithFormat:#"%#%d:", newIndent, i];
dumpViewsRecursive (subView, msg, newIndent);
[msg release];
[newIndent release];
}
}
+ (void) dumpViews: (UIView *) view {
dumpViewsRecursive (( (!view) ? [[UIApplication sharedApplication] keyWindow] : view), #"" ,#"");
}
+ (void) dumpViews: (UIView *) view from:(NSString *) fromText{
dumpViewsRecursive ((!view) ? [[UIApplication sharedApplication] keyWindow] : view, fromText, #"");
}
yes, the methods provided below will be called whenever a textField becomes Active. I think you are looking for
- (BOOL) textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return 1;
}
or
- (void) textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
- (void) textFieldDidEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
and if you are looking for a specific textField in your view, you should assign them tags:
textField.tag =1 // for textField 1
textField.tag =2 // for textField 2
// You may check for these tags and then resign specific ones.

UITableView losing height when i search

Im getting a little trouble here. I have a UISearchBar that searchs for me some content, that are included on UITableView. Perfect.
When my view loads, i set a 110 height to my table view row. Nice. When my view appears, it comes with 110 px. of height. Nice.
But when i click on UISearchBar, my rows loses the height setted.
I dont know what can be.
Any help, thanks!
- (void)viewDidLoad {
[super viewDidLoad];
theTableView.rowHeight = 110;
theTableView.backgroundColor = [UIColor clearColor];
}
And the search code:
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
[searchBar setShowsCancelButton:YES animated:YES];
self.theTableView.allowsSelection = NO;
self.theTableView.scrollEnabled = NO;
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text=#"";
[searchBar setShowsCancelButton:NO animated:YES];
[searchBar resignFirstResponder];
self.theTableView.allowsSelection = YES;
self.theTableView.scrollEnabled = YES;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
// All the actions goes here, like parsing xml.
[self.tableData removeAllObjects];
[theTableView reloadData];
}
Thanks!