从nib文件加载UIView问题

问题描述:

我正在开发一个具有多个视图的应用程序。我有一个图像库模板,我已经在一个xlib文件中创建。该视图将作为单个页面加载到滚动视图中。我能得到加载多个时间从Xlib中的观点如下:从nib文件加载UIView问题

- (void)registerForKeyboardNotifications 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWasShown:) 
               name:UIKeyboardDidShowNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(keyboardWillBeHidden:) 
               name:UIKeyboardWillHideNotification object:nil]; 

} 
- (id)initWithFrame:(CGRect)frame 
{ 
    self = [[[NSBundle mainBundle] loadNibNamed:@"GSEstimateView" owner:self options:NULL] lastObject]; 
    self.commentText.delegate = self; 
    self.scrollView.delegate = self; 
    self.commentText.delegate =self; 
    [self registerForKeyboardNotifications]; 
    return self; 
} 

我现在面临的第一个问题是,当键盘显示的keyboardWasShown:方法获取调用尽可能多UIViews我有创建。如果我尝试从第二个UIView加载键盘,我会得到一个被调用的无效选择器的异常。 UIView是从一个nib或xlib Singleton加载的吗?如何通过从nib文件加载它来通知我的UIView实例?

(^。^)“嗨,对不起我的英语不是很好,如果有人喜欢纠正我的新版本我将不胜感激这个”

嗨第一,我不建议使用NSNotification喜欢使用这样的协议。

@protocol KeyBoardDelegate <NSObject> 
- (void)KeyBoardVisible:(BOOL)op; 
@end 

如果你有数倍的观点,如果你想现在这个样子的视图控件:

  • * viewDidLoad中,viewDidUnload,viewWillDisappear,viewWillAppear中,和其他人*

我建议像这样使用UIViewController的视图。

UIViewControllerCustom *example = [[UIViewControllerCustom alloc] initWithNibName:@"exampleNIB" bundle:[NSBundle mainBundle]]; 
[self.view addSubview:example.view]; 

使用这个你可以采取例如视图控制器的视图的控制和使用的方法

- (void)viewDidLoad{ 
    [super viewDidLoad]; 
    //When the nib has been loaded. 
} 

- (void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    //When the view is show. 
} 

- (void)viewWillDisappear:(BOOL)animated{ 
[super viewWillDisappear:animated]; 
//The view is hidden 
} 

- (void)viewDidUnload{ 
[super viewDidUnload]; 
// Release any retained subviews of the main view. 
} 

而且很多方法。 :)

+0

将xlib视图添加为子视图绝对有意义。我将通过将键盘注册移动到init方法来尝试一下,看看它是如何发生的。可能是我会去第二个选项,如果这不起作用。谢谢你的帮助。 – 2012-04-23 15:08:20

+0

不客气。 – NTTake 2012-04-23 15:22:18