2012-01-06 44 views
0

在UITableViewController的didSelectRowAtIndexPath下,我將一個NSObject文件中的屬性設置爲一個值。我可以使用NSLog並查看變量確實已設置。但是,當我使用表視圖並單擊激活didSelectRowAtIndexPath的單元格時,我注意到當視圖更改爲另一個UITableViewController時,該變量將其自身設置爲零。我甚至在viewDidLoad下做了一個NSLog來檢查這個屬性值,它返回nil。爲什麼是這樣?我怎樣才能做到這一點,所以它不會改變爲零?爲什麼我的變量設置爲零?

首先UITableView.m:

#import "enchantmentViewController.h" 
#import "levelViewController.h" 
#import "dataBrain.h" 

@interface enchantmentViewController() 

@property (nonatomic, strong) dataBrain *brain; 
@property (nonatomic, strong) levelViewController *level; 

@end 

@implementation enchantmentViewController 

@synthesize brain = _brain; 
@synthesize level = _level; 

- (dataBrain *)brain 
{ 
    if (!_brain){ 
     _brain = [[dataBrain alloc] init]; 
    } 
    return _brain; 
} 

- (levelViewController *)level 
{ 
    if (!_level){ 
     _level = [[levelViewController alloc] init]; 
    } 
    return _level; 
} 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    //warning Potentially incomplete method implementation. 
    // Return the number of sections. 
    return [self.brain enchantmentNumberOfSections]; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    //#warning Incomplete method implementation. 
    // Return the number of rows in the section. 
    return [self.brain enchantmentNumberOfCells:section]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"enchantment"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    cell.textLabel.text = [self.brain enchantmentCellText:indexPath]; 

    return cell; 

} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    return [self.brain enchantmentSectionData:section]; 
} 


/* 
// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 
*/ 

/* 
// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 
*/ 

/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [tableView deselectRowAtIndexPath:indexPath animated:YES]; 
    [self.brain convertSelectedTableCellToNumberOfPossibleEnchantments:indexPath]; 
    [self.brain test]; 
} 

@end 

二的UITableViewController代碼:

#import "levelViewController.h" 
#import "dataBrain.h" 

@interface levelViewController() 

@property (nonatomic, strong) dataBrain *brain; 

@end 

@implementation levelViewController 

@synthesize brain = _brain; 


- (dataBrain *)brain 
{ 
    if (!_brain){ 
     _brain = [[dataBrain alloc] init]; 
    } 
    return _brain; 
} 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#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 the number of rows in the section. 
    //NSLog(@"needing sections"); 

    NSLog(@"value: %@", [self.brain levelNumberOfCells]); 
    return [self.brain.numberOfLevelsForCurrentEnchantment integerValue]; 
    //[self.brain levelNumberOfCells]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"level"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (!cell) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    cell.textLabel.text = @"jo";//[self.brain levelCellText:indexPath]; 

    return cell; 
} 

@end 

[self.brain test]返回是有問題的變量的值。

+0

這可能是一個愚蠢的問題:什麼是「NSObject文件」? – 2012-01-06 01:54:56

+0

一個NSObject的子類的新文件,抱歉沒有明確說明。 – blake305 2012-01-06 01:56:10

+0

發佈一些代碼,它會幫助我們。 – esqew 2012-01-06 01:56:42

回答

1

所以每個viewController都有自己的大腦,當需要時它會自動分配。你在一個VC的大腦中設置一個變量,然後切換VC,所以現在當你詢問關於它的大腦的東西時,你在問一個不同的大腦 - 你沒有做任何改變。

+0

那麼xcode如何知道大腦只針對特定的VC?我不能爲兩個控制器使用相同的模型嗎? – blake305 2012-01-06 02:07:43

+0

你可以,但你沒有:實施後,每個VC分配自己的大腦。我沒有看到代碼中的任何東西都能將大腦從一個VC轉移到另一個VC,也沒有看到一些全局都涉及到。 – 2012-01-06 02:11:06

+0

'[self.brain convertSelectedTableCellToNumberOfPossibleEnchantments:indexPath];'在模型中設置屬性和'NSLog(@「value:%@」,[self.brain levelNumberOfCells]);'假設記錄屬性的值。 – blake305 2012-01-06 02:14:25

相關問題