2011-03-02 4 views
1

설정 변수 (이 경우 area)와 사용자가 누른 버튼에 따라 특정 .plist 파일을로드하려고합니다. .plist의 파일 형식은 area-buttonPressed.plist입니다. 다음 (현재 영역에 현재 단지에 따라 다름) 특정 .plist 파일을 호출하는 데 사용되는 코드는Obj-C로드시 사용자 정의 .plist 파일로드 - iPhone

- (void)viewDidLoad { 
[super viewDidLoad]; 
NSLog(@"viewDidLoad"); 
self.navigationItem.title = @"Species List"; 

} 

-(void)viewWillAppear:(BOOL)animated{ 
    NSLog(@"viewWillAppear"); 
    area = [[NSUserDefaults standardUserDefaults] stringForKey:@"mulValue"]; 
    NSLog(@"Area is %@",area); 
    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", area] ofType:@"plist"]; 
    NSLog(@"Path is %@",path); 
    NSMutableDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path]; 
    self.names = dict; 
    [dict release]; 
    NSArray *array = [[names allKeys] sortedArrayUsingSelector:@selector(compare:)]; 
    self.keys = array; 
} 

내 주요 문제는 그 내가있는 viewDidLoad에 viewWillAppear 현재의 코드를 넣어 경우, (NavigationController를 사용하고 있습니다.) 뒤로 돌아가서 설정 영역을 변경하고이 viewController로 다시 이동하면 다시 호출되지 않습니다. 결과적으로이 방법은 처음으로이 뷰를로드 할 때 훌륭하게 작동하지만 파일을 다시 빌드 할 때까지 그대로 유지됩니다.

내 주요 질문 :

  1. 설정하는 올바른 방법은 무엇는이 개 변수를 사용하여 NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", area] ofType:@"plist"]; (설정 변수 영역 마지막보기에서 누른 버튼의 값을 (내가) 그 값에 대한있는 NSString가) ?
  2. 보기가 표시 될 때마다 * 경로를 어떻게 업데이트합니까?
  3. 해결책이 될 경우 '뒤로'버튼을 누를 때마다보기를 언로드하려면 어떻게해야합니까? (사용자가이보기로 이동할 때마다 viewDidLoad가 호출되기 때문에 문제가 해결됩니다.)
사전에

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return [keys count]; 
} 

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ 
    NSString *key = [keys objectAtIndex:section]; 
    return key; 
    } 

// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    NSString *key = [keys objectAtIndex:section]; 
    NSArray *nameSection = [names objectForKey:key]; 
    return [nameSection count]; 
} 

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    NSUInteger section = [indexPath section]; 
    NSUInteger row = [indexPath row]; 

    NSString *key = [keys objectAtIndex:section]; 
    NSArray *nameSection = [names objectForKey:key]; 

    static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SectionsTableIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier] autorelease]; 
    } 

    cell.textLabel.text = [nameSection objectAtIndex:row]; 
    return cell; 
    } 

- (void)viewDidUnload{ 
    self.names = nil; 
    self.keys = nil; 
    [super viewDidUnload]; 
} 

- (void)dealloc { 
    [names release]; 
    [keys release]; 
    [super dealloc]; 
} 

감사 :

다음은 내 코드의 나머지 부분입니다!

편집 : 내 NSLog 년대의 말에 일부 정보 : 나는 테이블 뷰를로드 할 수있는 버튼을 누를 때마다

, 내 콘솔 "viewWillAppear"를 보여주고 NSLog(@"Area is %@",area); (이 경우 REGION1) 올바른 영역을 보여줍니다. REGION1 아무것도하지만,이 선택 될 때

NSLog(@"Path is %@",path); 중 하나

(null) 표시, 또는 아이폰 시뮬레이터/4.2 /.../.../ Region1.plist

답변

0

I 돈 (난 단지 Region1.plist 지금까지를했습니다) viewWillAppear:에 코드가있는 문제를 확실히 이해하지 못했습니다. viewDidLoad은 접근 방식에 따라보기가 XIB에서 또는 -loadView 이후에 보관 취소 된 후에 호출됩니다.

보기가 나타날 때마다 업데이트해야하는 경우 viewWillAppear:이이 코드를 삽입 할 올바른 위치입니다. 이것이 작동하지 않는다면 NSUserDefaults가 설정보기에서 업데이트되고이보기로 다시 푸시되기까지 동기화 할 시간이 없었기 때문일 수 있습니다. 일반적으로 약 30 초마다 동기화가 발생하지만 [NSUserDefaults synchronize]을 사용하면 강제로 동기화 할 수 있습니다.

+0

NSUserDefaults를 사용할 때 NSUserDefaultsDidChangeNotification이 기본값이 변경된 경우 특히 알릴 수 있습니다. – Anomie

+0

viewWillAppear이이 작업을 수행 할 수있는 적절한 위치 여야한다는 것에 동의합니다. 또한 NSUserDefaults가 동기화됩니다 (InAppSettingsKit을 사용하고 있습니다). 그러나 지금까지 운이 없다. NSLogs에 대한 더 자세한 정보는 메인 포스트의 편집을 참조하십시오. – Moloch

+0

나는 당신이 가지고있는 특정한 문제에 관해서는 여전히 명확하지 않지만, 존재하지 않는 .plist를로드하려고 시도 할 수 있다고 생각합니까? 따라서'path'는 nil이되고'dict'은 nil이되어'self.names'가되고'self.keys'도 역시 nil이됩니다. –

관련 문제