从导航栏标题加载字典

时间:2012-02-20 15:06:47

标签: ios mkmapview uinavigationbar nsdictionary

我有一个mainViewController,它将detailViewController推送到屏幕上。 detailViewController中的项目(图像,导航栏标题,textview和按钮)都是从MainViewController中的NSMutableDictionary加载的,具体取决于它中的哪个图像被点击。

我还在detailViewController中有一个按钮,当选中它时会加载一个显示用户位置的mapview。当用户点击一个按钮时,一个引脚被丢弃,用注释标注标题标记该位置。

我希望mapview的标题和注释标题与从MainViewController的NSMutableDictionary加载的detailViewController相同。

现在我正在尝试使用detailViewController的标题作为mapview标题和注释标题中的决定因素。但是这不起作用。

这是我在MainViewController .m。

中的代码
- (void) coverflowView:(TKCoverflowView*)coverflowView coverAtIndexWasDoubleTapped:(int)index{

    SomeDetailViewController *detailViewController = [[SomeDetailViewController alloc] initWithNibName:@"SomeDetailViewController" bundle:nil];

    if ((int)index == 0) {
        NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                        @"0002.jpg", @"imagekey",
                        @"This is tree 0002", @"textkey", 
                        @"0002.image.png",@"imagekey2",
                        @"0002 title", @"headerkey",
                        @"www.0003.com", @"address",nil];

        NSLog(@"%@", [myDictionary objectForKey:@"imagekey"]);
        NSLog(@"%@", [myDictionary objectForKey:@"textkey"]);
        NSLog(@"%@", [myDictionary objectForKey:@"imagekey2"]);
        NSLog(@"%@", [myDictionary objectForKey:@"address"]);
        NSLog(@"%@", [myDictionary objectForKey:@"headerkey"]);
         detailViewController.myDictionary = myDictionary;
    }

    else if ((int)index == 1) {
        NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                             @"0003.jpg", @"imagekey",
                                             @"This is 0003", @"textkey", 
                                             @"0003.image.png",@"imagekey2",
                                             @"0003 title", @"headerkey",
                                             @"www.0003.com", @"address",nil];

        NSLog(@"%@", [myDictionary objectForKey:@"imagekey"]);
        NSLog(@"%@", [myDictionary objectForKey:@"textkey"]);
        NSLog(@"%@", [myDictionary objectForKey:@"imagekey2"]);
        NSLog(@"%@", [myDictionary objectForKey:@"address"]);
        NSLog(@"%@", [myDictionary objectForKey:@"headerkey"]);
        detailViewController.myDictionary = myDictionary;
    }

    [self.navigationController pushViewController:detailViewController animated:YES];
}

这是detailViewController.m中的代码。 这会加载我的Dictionary字符串(粗体部分正在加载导航栏标题)。

- (void)viewDidLoad {
    NSString *filePath =[self pathOfFile];
    if ([[NSFileManager defaultManager]fileExistsAtPath:filePath]){
        NSArray *array = [[NSArray alloc]initWithContentsOfFile:filePath];
        myText.text = [array objectAtIndex:0];
    }

    UIApplication *app = [UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:app];
    [super viewDidLoad];

    imageView .image = [[UIImage alloc] init];
    self.imageView.image =[UIImage imageNamed:[(NSMutableDictionary *)myDictionary objectForKey:@"imagekey"]];

    imageView2 .image = [[UIImage alloc] init];
    self.imageView2.image =[UIImage imageNamed:[(NSMutableDictionary *)myDictionary objectForKey:@"imagekey2"]];

    self.myText.text =[(NSMutableDictionary *)myDictionary objectForKey:@"textkey"];

    **self.navigationItem.title = [(NSMutableDictionary *)myDictionary objectForKey:@"headerkey"];**
    NSLog(@"%@", [myDictionary objectForKey:@"imagekey"]);
    NSLog(@"%@", [myDictionary objectForKey:@"imagekey2"]);
    NSLog(@"%@", [myDictionary objectForKey:@"textkey"]);
    NSLog(@"%@", [myDictionary objectForKey:@"headerkey"]);
}

以下是我尝试使用当前导航栏标题作为决定因素来加载导航栏标题和注释标题的位置。 这是我唯一可以考虑使用的东西,因为没有稳定的元素。 detailViewController中的所有内容都是从MainViewController NSDictionary加载的。

 -(IBAction)pushMap:(id) sender{

    SomePositionViewController *somePosition = [[SomePositionViewController alloc] init]; 


    if ((self.navigationItem.title = @"0002 title")) {
        NSMutableDictionary *mapDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                              @"0002a title", @"headerkey2",
                                             @"0002 annotation title", @"titlekey",nil];
        treePosition.mapDictionary = mapDictionary;
        NSLog(@"%@", [mapDictionary objectForKey:@"titlekey"]);
        NSLog(@"%@", [mapDictionary objectForKey:@"headerkey2"]);
    }

   else if((self.navigationItem.title = @"0003 title")) {
        NSMutableDictionary *mapDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                              @"0003a title", @"headerkey2",
                                              @"0003 annotation title", @"titlekey",nil];
        treePosition.mapDictionary = mapDictionary;
        NSLog(@"%@", [mapDictionary objectForKey:@"titlekey"]);
       NSLog(@"%@", [mapDictionary objectForKey:@"headerkey2"]);
    }

    [self.navigationController pushViewController:somePosition animated:YES];    

                                                }

有谁知道我怎么能做到这一点?我一直在绞尽脑汁但无济于事。帮助!

1 个答案:

答案 0 :(得分:0)

我发现的一件事是你正在以错误的方式比较字符串。你用

if ((self.navigationItem.title = @"0002 title"))

但应该使用

if (([self.navigationItem.title isEqualToString:@"0002 title"]))

按照您的方式比较字符串将始终失败。