2013-03-30 3 views
0

사용자가 입력하는 내용을 저장해야하는 ipad에 대한 마스터 세부 응용 프로그램을 만듭니다 (팝업보기의 레이블에 텍스트를 넣는 팝업보기의 단추 사용) 마스터 뷰 컨트롤러의 테이블보기 셀에 있습니다.Xcode는 NSUserDefaults를 사용하여 마스터보기 컨트롤러에 데이터를 저장합니다.

그래서 기본적으로 나는 inputDisplayLabel이라는 UILabel을 포함하는 popover 뷰를 가지고 있으며 사용자가 버튼 텍스트를 누르면 UILabel에 들어간 버튼을 누릅니다. 입력 버튼을 누르면 해당 텍스트가 테이블 뷰 셀에 추가됩니다. 이제 그 테이블 뷰에있는 내용을 저장하려고합니다. 응용 프로그램을 종료하고 다시 시작하면 텍스트가 여전히 tableview cel에 남아있게됩니다.

나는 SaveValue라는 버튼을 추가하려고하고 그래서 사장님 코드 :

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
self.tableView = [defaults objectForKey:@"SavedValue"]; 

하지만 지금 나는 다음과 같은 코드로 loadValue라는 또 다른 버튼을 추가로로드 시도

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
[defaults setObject:self.inputDisplayLabel.text forKey:@"savedValue"]; 
[defaults synchronize]; 

나던. 응용 프로그램이 시작될 때 거기에 있도록로드 코드를 실제로 어디에 두어야합니까? 그래서 다른 단추를 사용하지 않아도됩니다.

누군가가 나를 도울 수 있기를 바랍니다. 감사.

편집 :

-(void)setTheTextOfLabelWith:(NSString *)str 
{ 
    text = str; 
    if (!_objects) { 
     _objects = [[NSMutableArray alloc] init]; 
    } 
    //[_objects insertObject:[NSDate date] atIndex:0]; 
    [_objects insertObject:[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)] atIndex:0]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 


    [self.inputViewPopover dismissPopoverAnimated:YES]; 
} 

답변

1

목표 C 민감한 "savedValue 그렇다고"사건이 "savedValue 그렇다고"다르다. 물론 테이블 뷰는 문자열을 직접 가져올 수 없습니다. 문자열/텍스트를 예 : tableviewcell의 레이블. viewDidLoad에 코드를 넣으십시오. 레이블의 텍스트를 tableViewCell에 설정하면 "cellForRowAtIndexPath"메소드에서 레이블의 텍스트를 설정해야합니다. 어쩌면 viewcontroller 라이프 사이클과 UITableViewDelegate 및 DataSource 프로토콜을 살펴보아야합니다.

// 편집 : 기본적으로 동적 테이블 뷰가 있고 여러 셀에 사용자 기본값으로 저장 한 텍스트가 표시되기를 원하십니까? 동적 테이블 뷰를 사용하려면 일반적으로 Tableview를 표시/관리하는 ViewController에 UITableViewDataSource 및 UITableViewDelegate 프로토콜을 구현합니다. 여기에서 UITableViewDataSource 프로토콜의 "cellForRowAtIndexPath"메서드를 사용하여 셀을 만듭니다. 둘 이상의 항목을 저장하려면 배열을 사용하여 모든 사용자 기본값을 저장하고 cellForRow ..에서 반복하여 셀의 모든 텍스트를 가져옵니다.

예.

#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. 
    return self.yourArray.count; 
} 

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

    if(theCell == nil) 
    { 
     theCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier]; 
    } 

    theCell.textLabel.text = [self.yourArray objectAtIndex:indexPath.row]; 

    return theCell; 
} 

당신은 사용자 기본값이 일반적으로 오히려 설정의 어떤 종류를 저장하는 데 사용 (그러나 단지 일부 한, 정말 많은 양의 데이터를 저장 CoreData하거나 데이터를 저장하는 XML의 일종에 대해 생각하려면 "텍스트"는 완전히 작동해야합니다).

// edit2 : 프로젝트를 확인한 후 NSUserDefaults에 레이블을 저장하려고했으나 불가능했습니다. 이러한 레이블의 텍스트를 저장 한 다음 NSUserDefaults에서 레이블을로드 한 후이 텍스트로 레이블을 복원해야합니다.

- (IBAction)load 
{ 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

    NSArray *texts = [defaults objectForKey:@"items"]; 
    for(NSString *currentText in texts) 
    { 
     [self setTheTextOfLabelWith:currentText]; 
    } 
} 

- (void)savingData 
{ 
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

    NSMutableArray *textToSave = [[NSMutableArray alloc] init]; 
    for(UILabel *currentLabel in _objects) 
    { 
     [textToSave addObject:currentLabel.text]; 
    } 

    [defaults setObject:textToSave forKey:@"items"]; 
    [defaults synchronize]; 
} 

당신은 또한 당신의 메일에 대한 응답으로 해당 솔루션을 찾을 수 있습니다 가장 쉬운 솔루션 (또는 많은 수정없이 적어도 하나) 부하를 수정하고 그래서 같은 방법을 저장하는 것입니다.

+0

빠른 답장을 보내 주셔서 감사합니다. 테스트 할 화면의 어딘가에 레이블을 추가하려고 시도했지만 작동했지만 실제로 tableviews cel과 작동하도록하고 싶습니다. 그걸 도와 줄 수있어? –

+0

내 수정 된 답변보기 http://developer.apple.com/library/ios/ipad/#documentation/UIKit/Reference/UITableViewDelegate_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDelegate 및 http : //에서 살펴보십시오. developer.apple.com/library/ios/ipad/#documentation/UIKit/Reference/UITableViewDataSource_Protocol/Reference/Reference.html#//apple_ref/occ/intf/UITableViewDataSource – d4Rk

+0

코드를 편집했는데, 그 의미가 무엇입니까? SetTheTextOfLabelWith는 문자열의 인수를 취하고 사용자가 추가 단추를 누를 때마다이를 테이블 뷰 셀에 추가합니다. 그런 다음 NSUserDefaults를 사용하여 해당 셀을 저장하려고합니다. –

관련 문제