2011-11-14 5 views
-1

私は3つのボタンがあるメインページを作成しました。
これらのボタンに値0,1,2を割り当てました。
ユーザーがボタンをクリックすると、その値はプログラムで作成されたplistファイルに書き込まれます。plistから値を取得したときに表示するには、ログインページを表示するだけです

これらの値をplistファイルから正常に取得し、ログに出力することができます。
ユーザーがbutton1をクリックすると、メインページを非表示にしてログインページを表示します。
plistファイルが削除された場合、button1をクリックすると、メインページの表示が続行されます。

誰かがコードやヒントを教えてください。

答えて

0
//function to write into plist 
-(void)WriteFileToPlist:(int) num 
{  
//write everything in the plist 

    NSString *errorDesc; 
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:strArray format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc]; 
    if (plistData) { 
     [plistData writeToFile:[self savePathForFile:num] atomically:YES];    
    } 
} 

//save the path 
-(NSString *) savePathForFile:(int) num 
{ 
    NSArray *pathArray = 
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"myPlist%d.plist",num]]; 
} 

//get the data from plist 
- (NSArray *) GetFileDataFromPlist:(NSString *) fileName 
{ 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"]; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    //see if Data.plist exists in the Documents directory 
    if (![fileManager fileExistsAtPath:[self savePath:fileName] ]) { 
     [fileManager copyItemAtPath:path toPath:[self savePath:@""] error:nil]; 
    } 

    NSData *plistData = [[NSFileManager defaultManager] contentsAtPath:[self savePath:fileName]]; 
    NSString *error; NSPropertyListFormat format; 
    NSArray *heats = [NSPropertyListSerialization propertyListFromData:plistData 
                 mutabilityOption:NSPropertyListImmutable 
                   format:&format 
                 errorDescription:&error]; 
    if (!heats) { 
     NSLog(@"Failed to read myPlist. Error: %@", error); 
     [error release]; 
    } 
    return heats; 
} 

//getting the data form the saved path. 
-(NSString *) savePath:(NSString *) fileName 
{ 
    NSArray *pathArray = 
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    return [[pathArray objectAtIndex:0] stringByAppendingPathComponent:[NSString stringWithFormat:@"myPlist%@.plist",fileName]]; 
} 

2番目のボタンをタップすると、スーパービューから最初のボタンビューが削除され、2番目のボタンクリックにリンクされたビューが読み込まれます。

あなたはpresentModalViewControllerを使用してビューを提示することができます。..

を私はこれが役立つことを願っています。

+0

優秀、ありがとうございます –

+0

@VinodVishwakarmaありがとう、plsは正解とマークします。 –

関連する問題