【问题标题】:calling view controller string from imagePickerController delegate method从 imagePickerController 委托方法调用视图控制器字符串
【发布时间】:2011-12-23 10:38:01
【问题描述】:

我现在遇到的问题是

我正在调用这个方法: - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

来自 vc1

我有一个来自 vc1 的 string1,但不能在该方法中调用它

因为图像委托方法是从不同的类调用的。

当我 NSLog(@"%@", string1);它只显示空

我想从图像委托方法中检索 string1。

有人知道怎么做吗?非常感谢。

来源:

从视图控制器

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

NSString *requestString = [[request URL] absoluteString];

NSLog(@"%@", requestString);

NSArray *components = [requestString componentsSeparatedByString:@":"];

for (int i=0; i< [components count]; i++) {
    NSLog(@"components %@", [components objectAtIndex:i]);
}

if([components count] <= 1) {
    return YES;
}

if ([(NSString *)[components objectAtIndex:0] isEqualToString:@"toapp"]) {

    NSLog(@"toapp %@", [components objectAtIndex:0]);
    // 1번째 문자열이 toApp인 경우

    if([(NSString *)[components objectAtIndex:1] isEqualToString:@"showphoto"]) {

        NSLog(@"showphoto %@", [components objectAtIndex:1]);
        // 2번째 문자열이 relationButton인 경우

        NSLog(@" objectAtIndex:2 %@", [components objectAtIndex:2]); // param2

        pictureName = [components objectAtIndex:2];

        //call photo library
        picker = [[UIImagePickerController alloc] init];
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        picker.delegate = self;
        picker.allowsEditing = NO;
        [self presentModalViewController:picker animated:YES];

        return NO;
    }
}
return YES;

}

我声明:

NSString *string1;

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    viewcontroller *v = [[viewcontroller alloc]init];
    NSLog(@"string1 %@", v.string1);
}

exc_bad_access

从控制台: string1 (null)

我想从控制台查看字符串 Helloworld。 我无法从 imagePickerController 委托方法中调用字符串图片名称。

【问题讨论】:

  • 你试过self.string1吗?如果你调试,打开 self 部分,你能看到字符串 Helloworld 吗?
  • 我认为这是因为委托方法是从不同的类调用的,因为它说 exec bad access on string1 我需要知道如何从 diff 类调用 string1
  • 如何删除我的帖子?我犯了一个错误。

标签: iphone delegates


【解决方案1】:

viewDidLoad 仅在(顾名思义)其视图加载后调用。当您以某种方式呈现 viewController 时,就会发生这种情况。例如。将其推送到 UINavigationController,将其添加到 UITabBarController 或以模态方式呈现。

如果您想在 viewController 加载其视图之前使用 string1,您应该将 string1 = @"Helloworld"; 放入 initinitWithNibName:bundle: 或用于初始化第二个 viewController 的任何内容


顺便说一句,您在 viewDidLoad 方法的开头忘记了 [super viewDidLoad]
而且你的类名是错误的,它们应该以大写字母开头。

【讨论】:

    【解决方案2】:

    NSLog(@"string1 %@", *string1); 不正确
    NSLog(@"string1 %@", string1); 正确

    格式%@ 需要NSString* 而不是NSStringstring1NSString*


    更新:我注意到您将部分替换为:

    viewcontroller *v = [[viewcontroller alloc]init];
    NSLog(@"string1 %@", v.string1);
    

    这是不正确的。您在viewDidLoad 中初始化string1,但您使用从未加载过的vstring1。而且我认为您不应该创建一个新的 vc 对象。如果您需要一个常量字符串,请继续使用@"Helloworld"。如果你想要当前对象的字符串,你应该使用self.string1而不是创建一个新对象。请注意,Objective-C 中没有 static variable

    让我们试试这个:

    1. 确保在vc的.h文件中写入NSString * string1;
    2. string1 = @"Helloworld";替换为string1 = [[NSString alloc] initWithFormat:@"%@",@"Helloworld"];
    3. NSLog(@"string1 %@",self.string1);替换代理方法主体

    【讨论】:

    • 绝对正确。但只是让你知道,问题已经改变了。我只是发布这个,所以没有人给你 -1,因为你的答案对修改后的问题没有意义。
    • 谢谢马蒂亚斯。在我输入之前的答案时,问题发生了变化。好尴尬……
    【解决方案3】:

    只需要[保留字符串1];

    不需要视图控制器 *v = [[viewcontroller alloc]init];

    谢谢。

    【讨论】:

      猜你喜欢
      • 2011-06-26
      • 2015-08-22
      • 2011-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多