2011-11-14 23 views
-1

我已经创建了一个主页,其上有三个按钮。
我已经为这些按钮分配了值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]]; 
} 
在点击的第二个按钮从上海华移除第一按钮视图,然后加载链接到第二按钮点击视图

你可以使用目前的presentModalViewController观点..

我希望这将有助于。

+0

非常好,非常感谢你 –

+0

@VinodVishwakarma谢谢,请标记为正确答案。 –