无法使用 NSObject 在单独的 ViewController 中加载 UIView
该应用程序是基于视图的项目类型。 我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现单例是不正确的。
它应该是这样的:
在您的代码中,topBarObject 永远不会启动,因此每次调用 GetTopBarObject 时,它都会生成一个新的 TopBarObject 并返回它...
I see that the singleton is incorrect.
It should be something like this:
In your code the topBarObject is never initiated and therefore each time GetTopBarObject is called it generates a new TopBarObject and returns it...
它在 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]