iphone - viewcontroller 中 appdelegate 和 delegate 的区别

标签 iphone objective-c delegates uinavigationcontroller uiapplicationdelegate

我想将一个示例代码与(例如 SampleCode 项目)我的 iPhone 应用程序集成。在 firstViewController 中的示例代码中添加到 MainWindow.xib 中并链接到 viewController在下面的代码中创建。

@interface SampleCodeAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    firstViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet firstViewController *viewController;

和 viewController 使用 initWithCoder 实例化,当 firstView 出现在点击按钮时,可以通过调用 OpenCamera 方法打开相机,如下面的代码所示。

//in firstViewController.mm file

- (id)initWithCoder:(NSCoder *)coder {

    if (self = [super initWithCoder:coder]) {

    }

    [self initLocal];

    return self;
}
//to open camera in SampleCode application
    - (IBAction)OpenCamera {

        UIApplication *app = [UIApplication sharedApplication];
        SampleCodeAppDelegate* delegate = [app delegate];
        UIViewController* uiViewCont = [delegate viewController];
        ((CCamera*)m_pCamera)->Camera(uiViewCont);
    }

在我的基于导航的应用程序 (MyApplication) 中,我想直接使用 ViewController MyApplicationViewControllerA 之一调用 SampleCode 的 firstViewController,而不添加到 MyApplicationAppDelegate。

所以我想如果我在 MyApplicationViewControllerA viewController 中创建委托(delegate),它应该作为 SampleCode 应用程序中的 appdelegate。 我无法打开相机,但在关闭相机后,我无法打开 MyApplication 的任何其他 View ,除了 MyApplicationViewControllerA。 我正在尝试无法呈现其他 View 的 pushViewController 和 modalViewController。

我对委托(delegate)并不感到困惑。所以我想知道 AppDelegate(在 SampleCodeAppDelegate : NSObject <UIApplicationDelegate> 中)和在其他 ViewController 中声明的委托(delegate)有什么区别。

最佳答案

我正在尝试无法呈现其他 View 的 pushViewController 和 modalViewController。

回答: 如果您无法在相机覆盖屏幕后显示其他 View ,那么我认为您需要通过使用 performSelectorOnMainThread 在主线程上执行操作来显示其他 View

[self performSelectorOnMainThread:@selector(showOtherView) withObject:OtherViewControllerObj waitUntilDone:YES];

在 showOtherView 选择器方法中,首先你需要确认你应该将 viewconroller 推送到 navigationconroll 中,如果它已经被推送,那么你应该尝试使用 presentModalViewController 显示其他 View ,如下面的代码。

-(void)showOtherView{

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

[self presentModalViewController:OtherViewControllerObj animated:YES]; 

}

我认为这应该可行。

关于iphone - viewcontroller 中 appdelegate 和 delegate 的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8377618/

相关文章:

ios - 从一个 viewController 调用方法,同时显示另一个 viewController

iPhone UIView动画最佳实践

iphone - 在 child 的-onExit方法中停止声音?

iphone - 访问 UIWindow 的 rootViewController?

ios - 动态单元格高度有时不反射(reflect)

java - Kotlin - 运行时的接口(interface)委托(delegate)

IOS查看容器: how to set the delegate

iphone - 使用返回并接受参数的 block

iphone - iPhone:我的应用程序需要支持多任务吗?

objective-c - 用[x] [y]访问我的malloc二维数组