SBTableAlert not working - iphone

First of all, I'm just starting on iPhone development. I'm trying to get an SBTableAlert working (see https://github.com/blommegard/SBTableAlert )
My initial setup is simple: I have a UIViewController with a button. On the button press, I do the following (as per the SBTableAlert example):
- (IBAction)myBtn_Press
{
SBTableAlert *alert;
alert = [[SBTableAlert alloc] initWithTitle:#"Apple Style" cancelButtonTitle:#"Cancel" messageFormat:nil];
[alert.view setTag:2];
[alert setStyle:SBTableAlertStyleApple];
MySecondViewController *myWGVC = [[MySecondViewController alloc] init];
[alert setDelegate:myWGVC];
[alert setDataSource:myWGVC];
[alert show];
}
MySecondViewController is declared as:
#interface MySecondViewController : NSObject <SBTableAlertDelegate, SBTableAlertDataSource>
which means it will function as a delegate for the table view. I also include the following (pasted from the example):
#implementation MySecondViewController
#pragma mark - SBTableAlertDataSource
- (UITableViewCell *)tableAlert:(SBTableAlert *)tableAlert cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (tableAlert.view.tag == 0 || tableAlert.view.tag == 1) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
} else {
// Note: SBTableAlertCell
cell = [[SBTableAlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
[cell.textLabel setText:[NSString stringWithFormat:#"Cell %d", indexPath.row]];
return cell;
}
- (NSInteger)tableAlert:(SBTableAlert *)tableAlert numberOfRowsInSection:(NSInteger)section {
if (tableAlert.type == SBTableAlertTypeSingleSelect)
return 3;
else
return 10;
}
- (NSInteger)numberOfSectionsInTableAlert:(SBTableAlert *)tableAlert {
if (tableAlert.view.tag == 3)
return 2;
else
return 1;
}
- (NSString *)tableAlert:(SBTableAlert *)tableAlert titleForHeaderInSection:(NSInteger)section {
if (tableAlert.view.tag == 3)
return [NSString stringWithFormat:#"Section Header %d", section];
else
return nil;
}
#pragma mark - SBTableAlertDelegate
- (void)tableAlert:(SBTableAlert *)tableAlert didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableAlert.type == SBTableAlertTypeMultipleSelct) {
UITableViewCell *cell = [tableAlert.tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
else
[cell setAccessoryType:UITableViewCellAccessoryNone];
[tableAlert.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
}
- (void)tableAlert:(SBTableAlert *)tableAlert didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(#"Dismissed: %i", buttonIndex);
}
The error message I'm getting is:
2013-04-25 00:13:35.389 MyTestProject[3386:c07] *** -[SBTableAlert tableView:cellForRowAtIndexPath:]: message sent to deallocated instance 0x682ed80
however I have no idea how to trace this or debug it. It appears it could have something to do with ARC, since the demo project doesn't use it, but I can't pinpoint how to fix this.
Any help is appreciated!

Try creating properties with strong attribute in your main UIViewController subclass for both alert and myWGVC objects. They seem to be deallocated because of ARC before the alert is presented on screen since there are no strong references to the alert's delegate/datasource and the alert itself.

Related

How do I retrieve UITableView row number of a UISwitch?

I have tried several approaches posted here, but I cannot get my table full of switches to return an index value for the cell of the changed switch. I am creating the view containing the table programmatically (no xib).
TableSandboxAppDelegate.m I instantiate the view controller in didFinishLaunchingWithOptions: with:
...
TableSandboxViewController *sandboxViewController = [[TableSandboxViewController alloc]
init];
[[self window] setRootViewController:sandboxViewController];
...
TableViewController.h file reads:
#interface TableSandboxViewController : UITableViewController
{
NSMutableArray *_questionOrder;
NSMutableArray *switchStates;
}
#end
TableViewController.m cellForRowAtIndexPath: reads:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
UISwitch *theSwitch = nil;
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"MainCell"];
theSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
theSwitch.tag = 100;
[theSwitch addTarget:self action:#selector(switchChanged:)
forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:theSwitch];
} else {
theSwitch = [cell.contentView viewWithTag:100];
}
if ([[switchStates objectAtIndex:indexPath.row] isEqualToString:#"ON"]) {
theSwitch.on = YES;
} else {
theSwitch.on = NO;
}
return cell;
TableViewController.m -(IBAction)switchChanged:(UISwitch *)sender reads:
UITableViewCell *theParentCell = [[sender superview] superview];
NSIndexPath *indexPathOfSwitch = [self.tableView indexPathForCell:theParentCell];
NSLog(#"Switch changed at index: %d", indexPathOfSwitch.row);
My log result is always "Switch changed at index: 0". I feel like the problem is in that CGPoint line where I've tried combinations of replacements for "sender" ([sender superview], [[sender superview]superview], etc). I don't feel like that line is pointing to the view that displays the table.
What am I doing wrong?
Note added 10/9, 9:15 EDT: my goal is to be able to handle about 100 yes/no questions in the table, so reuse is a key. I want to scroll and have the table the state of each switch, as well as be able to retrieve them when leaving the view.
Tags is an okay solution, but a little clumsy because the cells - and therefore their subviews - are continually being reused, changing their rows - and therefore the tags they need.
Instead, I generally keep one of these around:
- (NSIndexPath *)indexPathWithSubview:(UIView *)subview {
while (![subview isKindOfClass:[UITableViewCell self]] && subview) {
subview = subview.superview;
}
return [self.tableView indexPathForCell:(UITableViewCell *)subview];
}
Then when I get an IBAction:
- (IBAction)someSubviewAction:(id)sender {
NSIndexPath *indexPath = [self indexPathWithSubview:(UIView *)sender];
// carry on from here
}
You may set switch view tag to row index. Instead of theSwitch.tag = 100;
do
-(UITableViewCell*)tableView:table cellForRowAtIndexPath:indexPth
{
UISwitch *theSwitch = nil;
if (cell == nil) {
...
// as per your example
[cell.contentView addSubview:theSwitch];
} else {
theSwitch = subviewWithClass(cell.contentView, [UISwitch class]);
}
theSwitch.tag = indexPath.row;
...
}
Add this helper function to replace viewWithTag: call
UIView *subviewWithClass(UIView *contentview, Class klass)
{
for (UIView *view in contentview.subviews)
if ([view isKindOfClass:klass])
return view;
return nil;
}
Then retrieve tag, that is a row index now, in your switchChanged function
-(IBAction)switchChanged:(UISwitch *)sender {
NSLog(#"Selected Switch - %d", sender.tag);
...
}
If you use something block-based (like https://github.com/brightsoftdev/iOS-Block-Based-Bindings/blob/master/UISwitch%2BBindings.m), you don't need to worry about getting the row, because you can reference the indexPath that is passed into tableView:cellForRowAtIndexPath: in your block.
Similar to #danh, I've come up with this solution using an extention which I've used multiple times.
#interface UIView (Find)
- (id)findSuperviewOfClass:(Class)class;
- (NSIndexPath *)findIndexPath;
#end
#implementation UIView (Find)
- (id)findSuperviewOfClass:(Class)class
{
return [self isKindOfClass:class] ? self : [self.superview findSuperviewOfClass:class];
}
- (NSIndexPath *)findIndexPath
{
UITableView *tableView = [self findSuperviewOfClass:[UITableView class]];
return [tableView indexPathForCell:[self findSuperviewOfClass:[UITableViewCell class]]];
}
#end
for iOS6+ you could maintain a NSMutableArray queuedSwitches
in -tableView:cellForrowAtIndexPath: you would take a switch, if not empty and places it on the custom cell and assign it to a property. If empty you create a new one.
in -tableView:didEndDisplayingCell:forRowAtIndexPath: you would add it to quededSwitches and remove it from it cell.
This will just allocate enough switches for visible cells and reuse them.
the switches are all wired up to one action.
-(void)switchAction:(UISwitch *)switch
{
NSIndexPath *indexPath = [self.tableView indexPathForCell:[switch superView]];
//…
}
You could create a subclass of UISwitch and add an indexPath property, then just set the indexPath in cellForRowAtIndexPath:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SwitchCell *returnCell = [tableView dequeueReusableCellWithIdentifier:#"SwitchCell" forIndexPath:indexPath];
returnCell.switch.indexPath = indexPath;
return returnCell;
}

How to make SearchBar in tableview work?

So i am trying to make an application that displays names of people in a tableview and on tap moves to the next view controller that shows an image of the person.
However when i add the search bar on the table view; i dont seem to have it right.
What am i doing wrong here?
The code compiles and displays on the simulator but when i click on any of the buttons, it gives me the errors i hate the most (Thread 1 : signal SIGABRT)
Here is my code for the Table View Controller
#import "PhotoTableViewController.h"
#import "Photo.h"
#import "DisplayViewController.h"
#interface PhotoTableViewController ()
#end
#implementation PhotoTableViewController
#synthesize photoSearchBar, showPhotos, filteredPhotos;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
showPhotos = [NSArray arrayWithObjects:
[Photo photoofname:#"Main" filename:#"photo1.jpg" notes:#"Amazing Road Bike"],
[Photo photoofname:#"Back" filename:#"photo3.jpg" notes:#"this is the back"], nil];
[self.tableView reloadData];
self.filteredPhotos = [NSMutableArray arrayWithCapacity:[showPhotos count]];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.searchDisplayController.searchResultsTableView) {
return [filteredPhotos count];
} else {
return [showPhotos count];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Photo *photo = nil;
if (tableView == self.searchDisplayController.searchResultsTableView)
{
photo = [filteredPhotos objectAtIndex:indexPath.row];
}else
{
photo = [showPhotos objectAtIndex:indexPath.row];
}
cell.textLabel.text = photo.name;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
#pragma mark Content Filtering
-(void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
[self.filteredPhotos removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"SELF.name contains[c] %#",searchText];
filteredPhotos = [NSMutableArray arrayWithArray:[showPhotos filteredArrayUsingPredicate:predicate]];
}
#pragma mark - UISearchDisplayController Delegate Methods
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
// Tells the table data source to reload when scope bar selection changes
[self filterContentForSearchText:self.searchDisplayController.searchBar.text scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
#pragma mark - TableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Perform segue to candy detail
[self performSegueWithIdentifier:#"candyDetail" sender:tableView];
}
#pragma mark - Segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"photoDetail"]) {
UIViewController *candyDetailViewController = [segue destinationViewController];
// In order to manipulate the destination view controller, another check on which table (search or normal) is displayed is needed
if(sender == self.searchDisplayController.searchResultsTableView) {
NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
NSString *destinationTitle = [[filteredPhotos objectAtIndex:[indexPath row]] name];
[candyDetailViewController setTitle:destinationTitle];
}
else {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *destinationTitle = [[showPhotos objectAtIndex:[indexPath row]] name];
[candyDetailViewController setTitle:destinationTitle];
}
}
}
Also this is the code for my Objective C Class called Photo
#import "Photo.h"
#implementation Photo
#synthesize name,filename,notes;
+(id) photoofname: (NSString*)name filename:(NSString*)filename notes:(NSString*)notes{
Photo *newPhoto = [[Photo alloc]init];
newPhoto.name = name;
newPhoto.filename = filename;
newPhoto.notes = notes;
return newPhoto;
}
#end
Well, just by looking at the code what I can suggest you is, first remove that call to prepareForSegue method called in UITableView's delegate method, didSelectForRowAtIndexPath.
You are overriding prepareForSegue, so in your storyboard you should have a prototype cell from where you have to ctrl-drag to the destination controller and segue it accordingly. That's a basic concept. Still having problem? Let us see your console messages when it crashes.

All cells on UITableview change when I try to create a new one

I'm trying to create an iOS app in which I must have a UITableView that every time I press a new entry button, a newcell appears with the time when I pressed that button. My problem is that everytime I press the buttons, not only the cell which is created displays the current time, but the cells above it, which were showing a different time, reload and also show the current time. To try and explain it better, if I press the button at 8:05, 9:01 and 9:10, i want the UITableView to show:
-8:05
-9:01
-9:10
Instead, it's showing:
-9:10
-9:10
-9:10.
What do I do?? Thanks
Here's my code ( newEntry is the button and brain is an object where I have the method to get the current time)
#implementation MarcaPontoViewController{
NSMutableArray *_entryArray;
#synthesize brain=_brain;
- (void)viewDidLoad
{
[super viewDidLoad];
_brain = [[Brain alloc] init];
_entryArray = [[NSMutableArray alloc] init];
//[self updateTime];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [_entryArray count];
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier= #"myCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [_entryArray lastObject];
}
return cell;
}
- (IBAction)newEntry:(id)sender {
[_entryArray addObject:[self.brain currentTime]];
[_timeTable reloadData];
}
#end
Your problem is here in this line :
cell.textLabel.text = [_entryArray lastObject];
You need to use :
cell.textLabel.text = [_entryArray objectAtIndex:indexPath.row];
Or,
cell.textLabel.text = _entryArray[indexPath.row];
[_entryArray lastObject] always gives the last returned object.
Use
cell.textLabel.text = [_entryArray objectAtIndex: indexPath.row];
The line cell.textLabel.text = [_entryArray lastObject] will only ever return the last object in the array, which is why you are seeing the same time repeated. Change this to:
// in cellForRowAtIndexPath:
cell.textLabel.text = [_entryArray objectAtIndex:indexPath.row];
This should fix the underlying problem.

How to pass data back from detail view controller to uitableview?

In my app i making use of uitable to select category from my list.
my task is,when ever user click or select a cell he should be able to view the selected cell detail in next view(detail view). and when he select the item in a detail view he should be able to move back in a table view and should be able to see the selected item in a rootivew controller.
i am able to navigate properly from tableview to detail view,but i am not able to show the item which is selected in detail view to rootviewcontroller.
please help me out with this issue.
image one is my rootview controller page.
for example : if user select #"make" he will able to see all the releated category of #"make"
. in a next page(which image2).
image to is my detail page.
and when user select #"abarth" it should be display in a rootview controller page(which is page one).
following is my code of rootview controller page:
- (void)viewDidLoad
{
self.car = [[NSArray alloc]initWithObjects:#"Make",#"Model",#"Price Min",#"Price Max",#"State",nil];
[super viewDidLoad];
}
-(NSInteger) numberOfSectionInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.car count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *TextCellIdentifier = #"Cell";
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:TextCellIdentifier];
if (cell==nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TextCellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.textLabel.text = [self.car objectAtIndex:[indexPath row]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (0 == indexPath.row)
{
NSLog(#"0");
self.detailcontroller.title = #"Make";
}
else if (1 == indexPath.row)
{
NSLog(#"1");
self.detailcontroller.title = #"Model";
}
else if (2 == indexPath.row)
{
NSLog(#"2");
self.detailcontroller.title = #"Price Min";
}
else if (3 == indexPath.row)
{
self.detailcontroller.title = #"Price Max";
}
else if (4 == indexPath.row)
{
NSLog(#"3");
self.detailcontroller.title = #"State";
}
[self.navigationController
pushViewController:self.detailcontroller
animated:YES];
}
following is my detail view page code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([self.title isEqualToString:#"Make"])
{
detail = [[NSArray alloc]initWithObjects:#"Any Make",#"Abarth",#"AC",#"ADAYER",#"Adelaide",#"ALFA ROMEO",#"ALLARD",#"ALPINE-RENAULT",#"ALVIS",#"ARMSTRONG",
#"ASTON MARTIN",#"AUDI",#"AUSTIN",#"AUSTIN HEALEY",#"Barossa",#"BEDFORD",
#"BENTLEY",#"BERTONE",#"BMW",#"BOLWELL",#"BRISTOL",#"BUICK",#"BULLET",
#"CADILLAC",#"CATERHAM",#"CHERY",#"CHEVROLET",#"CHRYSLER",#"CITROEN",
#"Country Central",#"CSV",#"CUSTOM",#"DAEWOO",#"DAIHATSU",#"DAIMLER",
#"DATSUN",#"DE TOMASO",#"DELOREAN",#"DODGE",#"ELFIN",#"ESSEX",
#"EUNOS",#"EXCALIBUR",#"FERRARI",nil];
if ([self.title isEqualToString:#"Abarth"])
{
detail = [[NSArray alloc]initWithObjects:#"HI", nil];
}
}
else if ([self.title isEqualToString:#"Model"])
{
detail = [[NSArray alloc]initWithObjects:#"Any Model", nil];
}
else if ([self.title isEqualToString:#"Price Min"])
{
detail = [[NSArray alloc]initWithObjects:#"Min",#"$2,500",
#"$5,000",
#"$7,500",
#"$10,000",
#"$15,000",
#"$20,000",
#"$25,000",
#"$30,000",
#"$35,000",
#"$40,000",
#"$45,000",
#"$50,000",
#"$60,000",
#"$70,000",
#"$80,000",
#"$90,000",
#"$100,000",
#"$500,000",
#"$1,000,000",nil];
}
else if ([self.title isEqualToString:#"Price Max"])
{
detail = [[NSArray alloc]initWithObjects:#"Max",
#"$2,500",
#"$5,000",
#"$7,500",
#"$10,000",
#"$15,000",
#"$20,000",
#"$25,000",
#"$30,000",
#"$35,000",
#"$40,000",
#"$45,000",
#"$50,000",
#"$60,000",
#"$70,000",
#"$80,000",
#"$90,000",
#"$100,000",
#"$500,000",
#"$1,000,000",nil];
}
else if ([self.title isEqualToString:#"State"])
{
detail = [[NSArray alloc]initWithObjects:#"Any State",
#"Australian Capital Territory",
#"New South Wales",
#"Northern Territory",
#"Queensland",
#"South Australia",
#"Tasmania",
#"Victoria",
#"Western Australia",nil];
}
[self.tableView reloadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [detail count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [detail objectAtIndex:
[indexPath row]];
cell.font = [UIFont systemFontOfSize:14.0];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController popViewControllerAnimated:YES];
}
You need to make use of custom delegates. Create a protocol in your detailview and implement it in your rootview.Pass the selected string as parameter to delegate method and from the delegate method, display it in your textfield.
//something like this
#interface detailViewController
// protocol declaration
#protocol myDelegate
#optional
-(void)selectedValueIs:(NSString *)value;
// set it as the property
#property (nonatomic, assign) id<myDelegate> selectedValueDelegate;
// in your implementation class synthesize it and call the delegate method
#implementation detailViewController
#synthesize selectedValueDelegate
// in your didselectRowAtIndex method call this delegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self selectedValueDelegate])selectedValueIs:valueString] ;
[self.navigationController popViewControllerAnimated:YES];
}
#end
// In your rootViewController conform to this protocol and then set the delegate
detailViewCtrlObj.selectedValueDelegate=self;
//Implement the delegate Method
-(void)selectedValueIs:(NSString *)value{
{
// do whatever you want with the value string
}
Hi you will have to do it using protocols and delegate
Please see my linkon protocol and delegate
you can also do it by creating a variable in appdelegate , setting its property and accessing it in any other view .
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
delegate.yourVariable;

UITextField - crash on delegates that return BOOL

I have a UITextField that I am adding to a UITableViewCell to use as a search field for a long list of accounts. I have added it as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
if ((indexPath.section < accountsection) && (hasSearch) && (indexPath.row == 0))
{
// Search
if (!searchField)
{
searchField = [[UITextField alloc] initWithFrame:CGRectMake(20, 10, cell.frame.size.width - 40, 25)];
[searchField setEnabled:YES];
searchField.placeholder = NSLocalizedString(#"Search", #"search");
searchField.keyboardType = UIKeyboardTypeDefault;
searchField.returnKeyType = UIReturnKeySearch;
searchField.autocorrectionType = UITextAutocorrectionTypeNo;
searchField.clearButtonMode = UITextFieldViewModeAlways;
searchField.delegate = self;
[cell addSubview:searchField];
[searchField release];
}
// Clean up an account label if needed
cell.accessoryType = UITableViewCellAccessoryNone;
cell.textLabel.text = #"";
cell.detailTextLabel.text = #"";
// Show the search field if it was hidden by a text label
searchField.hidden = NO;
[cell bringSubviewToFront:searchField];
}
}
To detect edits to the text field, I have set up the UITextFieldDelegate in the header and trap the following delegate calls:
#interface AccountViewController : UITableViewController <UITextFieldDelegate> {
BOOL hasSearch;
UITextField *searchField;
...
}
In the implementation, I then handle these delegate methods:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(#"Done editing");
[self filterAccountsBy:textField.text];
[textField resignFirstResponder];
return NO;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSLog(#"Searching for %#", string);
[self filterAccountsBy:string];
return YES;
}
However in the second one, unless I return YES, the text never changes; in the first one, returning YES seems to not affect me. But when I return YES in either, I get a nasty EXC_BAD_ACCESS.
I must be missing something in my manual adding of this UITextField to my cell but I can't figure out what it is... can anyone assist?
Many thanks.
EDIT: As suggested below, I commented out the filterAccounts call and my app now no longer crashes. Here is the full code for this method:
- (void)filterAccountsBy:(NSString *)filterstring
{
[accounts removeAllObjects];
if (([filterstring length] == 0) && (!isChooser) && (![vpmsConn isDomainLogon])) {
[accounts addObject:[[vpmsConn accounts] objectAtIndex:0]];
}
if ([filterstring length] == 0) {
[accounts addObjectsFromArray:[cache accounts]];
} else {
for (AccountItem *ac in [cache accounts])
{
BOOL found = NO;
// Name search
if ([[ac.clientName uppercaseString] rangeOfString:[filterstring uppercaseString]].location != NSNotFound) {
found = YES;
}
//more similar searches
if (found) {
[accounts addObject:ac];
}
}
}
[self.tableView reloadData];
}
I am a bit confused, though. When I filter this list using the textFieldShouldReturn and then return NO, it filters properly and does not crash. Something about returning YES from either one of these methods causes the crash after I have filtered. If I have not filtered at all, returning YES is no problem.
Let me know if there is any other code that I should post.
I have resolved the issue but I cannot (due to NDA) post the answer here yet. Those interested, and in the paid iOS developer program, should go to the NDA developer forums and search for UITextField EXC_BAD_ACCESS and you will find the answer...
in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
where is
return cell;
?
I think your UITextField searchfield is begin deallocated. If you use self.searchField to set your searchfield (assuming it's a retained property), that will solve it.
I'm not sure what the rest of your .h looks like. You could do:
#property (nonatomic, retain) UITextField *searchField;
Then in the .m:
#synthesize searchField;
That's what I mean by making search field a retained property. This way your class will keep the search field around.
In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathreturn cell;
Donot release serachfield in the event;
Release in dealloc
add #property (nonatomic, retain) UITextField *searchField;