无法使用 NSObject 在单独的 ViewController 中加载 UIView

发布于 2024-08-18 14:58:37 字数 1393 浏览 10 评论 0原文

该应用程序是基于视图的项目类型。 我在 View XIB(UIView Nib) 中使用 NSObject,假设该对象是应用程序的顶栏。

MainController UIViewController 加载初始屏幕。 看起来像这样,

UIViewController MainViewController Image

我想加载一个顶视图(新的,调用X)在上图中的顶部栏视图(红色)中单独设计。

现在,我有 NSObject,它驻留在“X”内部并绑定所有事件并完全控制 X,它是一个单例对象。

在 NSObject 类中,我有一个方法返回 self 对象实例及其控制的 uiview("X") 实例。

方法是,

-(UIView*)ReturnTopBarView
{
            NSLog(@"called 2");
    return topBarObject.topBarView;
}

//Provides the top bar instance to all the view controllers.
//Singleton Implementation.
+ (id)GetTopBarObject
{
    if(topBarObject == nil)
    {
        NSLog(@"called 1");
        return [[TopBarObject alloc] init];
    }
    return topBarObject;    
}

topbarview 是完全设计的topbarview(“X”),它必须出现在主视图控制器的topbarview(上图中的红色部分)中。

我在主视图控制器中编写了一个方法:

-(void)SetHomeScreen
{
    self.topView = [[TopBarObject GetTopBarObject] ReturnTopBarView];
}

我在 appdelegate.applicationdidfinishlaunching 中调用 -(void)SetHomeScreen 作为 [viewcontroller SetHomeScreen]

这实际上调用了NSObject(TopBarObject 类)中的相应方法,但我没有看到视图更新 (主视图控制器.topbarview)。这是现在的主要问题。

我有一个带有图像和图标的自定义顶部栏和自定义底部栏。所以我选择了这种设计方式。 我不确定这是否是最好的方法,我只是在寻求最优化的系统设计以使这项工作高效。如果您认为还有其他方法比这好得多,请与我分享。

The app is a viewbased project type.
I am using a NSObject in a View XIB(UIView Nib), assume this object to be the topbar of the app.

MainController UIViewController loads the initial screen.
it looks like this,

UIViewController MainViewController Image

i wanted to load a Top view(new one, call it X) Designed separately in the topbarview(red colour) in the above image.

Now,I have NSObject which resides inside "X" and binds all the events and have full control over X, its a singleton object.

in the NSObject Class i have a method to return the self object instance and a instance of uiview("X") it controls.

The methods are ,

-(UIView*)ReturnTopBarView
{
            NSLog(@"called 2");
    return topBarObject.topBarView;
}

//Provides the top bar instance to all the view controllers.
//Singleton Implementation.
+ (id)GetTopBarObject
{
    if(topBarObject == nil)
    {
        NSLog(@"called 1");
        return [[TopBarObject alloc] init];
    }
    return topBarObject;    
}

The topbarview is the fully designed topbarview("X") which has to come in the main view controller's topbarview(red color segment in the image above).

i wrote a method in the main view controller as :

-(void)SetHomeScreen
{
    self.topView = [[TopBarObject GetTopBarObject] ReturnTopBarView];
}

i invoke the -(void)SetHomeScreen in my appdelegate.applicationdidfinishlaunching as [viewcontroller SetHomeScreen]

This actually calls the corresponding methods in the NSObject(TopBarObject class), but i dont see the view updated in
(mainviewcontroller.topbarview). This is the main issue now.

i have a custom top bar and custom bottom bar with images and icons. So i chose this way of designing.
I am not sure whether this is the best way, I am just seeking for a most optimal system design to make this work efficient. If you think there is some other way which is far far good than this please share it with me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅沫记忆 2024-08-25 14:58:37

我发现单例是不正确的。
它应该是这样的:

+ (id)GetTopBarObject {
    if(topBarObject == nil) {
        topBarObject = [[TopBarObject alloc] init];
    }
    return topBarObject;
}

在您的代码中,topBarObject 永远不会启动,因此每次调用 GetTopBarObject 时,它都会生成一个新的 TopBarObject 并返回它...

I see that the singleton is incorrect.
It should be something like this:

+ (id)GetTopBarObject {
    if(topBarObject == nil) {
        topBarObject = [[TopBarObject alloc] init];
    }
    return topBarObject;
}

In your code the topBarObject is never initiated and therefore each time GetTopBarObject is called it generates a new TopBarObject and returns it...

断念 2024-08-25 14:58:37

它在 appdelegate.applicationdidfinishlaunching 的哪里被调用?
如果您在视图实际设置之前调用,您所做的就是设置 nil 。
尝试一个
viewcontroller.view;

[视图控制器设置主屏幕]

Where in appdelegate.applicationdidfinishlaunching is it called ?
If you are calling before the view is actually setup all you do is setting nil with something.
Try a
viewcontroller.view;
before
[viewcontroller SetHomeScreen]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文