1
我還是比較陌生的對象c,所以如果這是一個菜鳥問題,請耐心等待。我試圖用相應的對象信息設置我的navigationcontroller的標題。我正在使用prepareforsegue,但第一次繼續使用新控制器,標題爲空。如果我再試一次,它會顯示出來,但是如果我按下其他的東西,它會顯示我以前按下的東西的標題。我在下面嵌入了我的代碼。Prepare reforsegue navigationItem.title one behind
//.h
#import <UIKit/UIKit.h>
@interface STATableViewController : UITableViewController
@property(strong,nonatomic)NSArray *listOfExercises;
@property(weak,nonatomic)NSString *navTitle;
@end
//.m
#import "STATableViewController.h"
#import "ExercisesViewController.h"
@implementation STATableViewController
@synthesize listOfExercises = _listOfExercises, navTitle = _navTitle;
- (void)viewDidLoad
{
[super viewDidLoad];
_listOfExercises = [NSArray arrayWithObjects:@"Raketstart",@"SpeedBåd",@"Træstamme",nil];
self.navigationItem.title = @"Exercises";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [_listOfExercises 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];
}
NSString *cellValue = [_listOfExercises objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
_navTitle = [_listOfExercises objectAtIndex:indexPath.row];
//NSLog(_navTitle);
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:@"toExercise"])
{
ExercisesViewController *foo = [segue destinationViewController];
foo.navigationItem.title= _navTitle;
}
}
@end
這工作,非常感謝你:) – 2012-03-15 23:04:34