How to pass an NSMutableString which is instantiated in an accessoryButtonTappedForRowWithIndexPath method to another view
I need a way to pass the variable "resultStringID" to the second view "DetailViewController" from "RootViewController" every time the user clicks the accessory button. Here is my code for the accessoryButtonTappedForRowWithIndexPath method
- (void)t开发者_JAVA百科ableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
processSelected = [array objectAtIndex:indexPath.row];
//realstatusSelected = [processStatusarray objectAtIndex:indexPath.row];
//NSLog(@"Real selected status is %@", realstatusSelected);
NSLog(@"combinedString at index is %@", processSelected);
NSMutableString * resultStringID = [NSMutableString stringWithCapacity:processSelected.length];
NSScanner *scanner = [NSScanner scannerWithString:processSelected];
//define the allowed characters, here only all numbers are allowed
NSCharacterSet *allowedChars = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
while ([scanner isAtEnd] == NO) {
NSString *buffer;
if ([scanner scanCharactersFromSet:allowedChars intoString:&buffer]) {
[resultStringID appendString:buffer];
} else {
[scanner setScanLocation:([scanner scanLocation] + 1)];
}
}
//Trim off the last 3 characters if the string is larger than 4 characters long
if ( [resultStringID length] > 4 )
resultStringID = [resultStringID substringToIndex:[resultStringID length] - 3];
Any help is very much appreciated! :)
The easiest way would be to add an @property (copy) NSString *myString
to your DetailViewController, then when you create the new controller (just before pushing it) set newController.myString = resultStringID
.
Do you want to present/push this DetailViewController? The typical way, would be create your own init method for DetailViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andResultId:(NSMutableString*)resultStringID
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Do stuff with your resultStringID
}
return self;
}
And then push it like always:
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle] andResultID:resultStringID];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
I assumed that you wanted to push it. If, on the contrary, the DetailViewController already exists, and you have a reference to it, just add another @property (nonatomic,retain) for your string.
精彩评论