【问题标题】:How to use presentModalViewController in iPhone/iPad?如何在 iPhone/iPad 中使用 presentModalViewController?
【发布时间】:2011-02-08 11:00:41
【问题描述】:

您好,我是 iPhone/iPad 开发的新手。

在我的应用程序中,单击按钮想要显示像 presentModalViewController 这样的视图控制器,并且我能够执行包含具有一些值的 UITableView 的操作。在选择微粒行时,我想将值传递给该控制器后面的控制器。

为此,我正在使用苹果示例应用程序 PhotoPicker 代码。 http://developer.apple.com/library/ios/#samplecode/PhotoPicker/Introduction/Intro.html

但我无法理解我在代码中做错了什么。

我无法进入 MyViewController.m 中的代码

- (void)didFinishWithCamera
{
    [self dismissModalViewControllerAnimated:YES];
//Here is my some logic
}

谁能帮我解决这个问题...如何从 OverlayViewController 调用这个函数?

请参考上面的链接并指导我或给我一些步骤或指导我。

【问题讨论】:

    标签: iphone ipad presentmodalviewcontroller


    【解决方案1】:

    使用delegation

    我现在正在编写的应用程序中使用了类似的东西:

    // MySecretSelectionViewController.m
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        [delegate mySecretSelectionViewController:self didSelectObject:[self objectForIndexPath:indexPath] atIndexPath:indexPath];
    }
    
    // MyViewController.m
    - (void)mySecretSelectionViewController:(MySecretSelectionViewController *)es didSelectObject:(MySecretObject *)object atIndexPath:(NSIndexPath *)indexPath {
        // do something with the selected object
        [self dismissModalViewControllerAnimated:YES];
    }
    
    - (void)showMySecretSelectionViewController:(id)sender {
        MySecretSelectionViewController *vc = ...
        vc.delegate = self;
        // present ViewController
    }
    

    【讨论】:

    • 我还是不明白你的意思。你能帮我更多吗?
    【解决方案2】:

    您也可以使用 NSNotificationCenter 来做到这一点。

    MyViewController.m 内部:

    - (void)viewDidLoad 
    {
        // your code
    
        // Add observers
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didFinishWithCamera) name:@"YourObserverName" object:nil];
    }
    
    + (void)callDidFinishWithCamera
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"YourObserverName" object:nil];
    }
    
    - (void)dealloc 
    {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    
        // your code
    }
    

    来自 OverlayViewController.m:

    [MyViewController callDidFinishWithCamera];
    

    使用上面的类方法从OverlayViewController调用MyViewController中的didFinishWithCamera

    【讨论】:

    • 从现在开始的 12 个月后,您将项目中的 50 个通知重构为委托。一个月前就这么做了^^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    相关资源
    最近更新 更多