2013-01-12 91 views
0

我想用下面的代碼加載自定義鍵盤的xib文件。之後,我需要卸載並重新加載。我不明白如何重新加載此代碼。我想知道該怎麼做。加載/重載xib uiview

- (id)init { 
UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation]; 
CGRect frame; 

if(UIDeviceOrientationIsLandscape(orientation)) 
    frame = CGRectMake(0, 0, 480, 162); 
else 
    frame = CGRectMake(0, 0, 320, 216); 

    self = [super initWithFrame:frame]; 

if (self) 

{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PMCustomKeyboard" owner:self options:nil]; 
    [[nib objectAtIndex:0] setFrame:frame]; 
    self = [nib objectAtIndex:0]; 

    [[NSBundle mainBundle] loadNibNamed:@"KeyBoardAccView_iPhone" owner:self options:nil]; 


} 

[self.altButton setTitle:kAltLabel forState:UIControlStateNormal]; 

[self.returnButton setTitle:kReturnLabel forState:UIControlStateNormal]; 
self.returnButton.titleLabel.adjustsFontSizeToFitWidth = YES; 

[self loadCharactersWithArray:kChar]; 

[self.spaceButton setBackgroundImage:[PMCustomKeyboard imageFromColor:[UIColor colorWithWhite:0.5 alpha:0.5]] 
      forState:UIControlStateHighlighted]; 
self.spaceButton.layer.cornerRadius = 7.0; 
self.spaceButton.layer.masksToBounds = YES; 
self.spaceButton.layer.borderWidth = 0; 
[self.spaceButton setTitle:kSpaceLabel forState:UIControlStateNormal]; 

return self; 
} 


-(void)setTextView:(id<UITextInput>)textView { 

if ([textView isKindOfClass:[UITextView class]]) 
    [(UITextView *)textView setInputView:self]; 
else if ([textView isKindOfClass:[UITextField class]]) 
    [(UITextField *)textView setInputView:self]; 

_textView = textView; 

NSLog(@"setTextView"); 
} 

-(id<UITextInput>)textView { 
return _textView; 
} 

回答

2

我剛剛張貼在如何從一個的.xib文件中加載自定義的UIView的樣本項目。

https://github.com/PaulSolt/CompositeXib

你需要一個名爲WidgetView類/文件的.xib額外的UIView電源插座和這個代碼:

- (id)initWithCoder:(NSCoder *)aDecoder { 
    self = [super initWithCoder:aDecoder]; 
    if(self) { 
     [self setup];  
    } 
    return self; 
} 

- (void)setup { 
    [[NSBundle mainBundle] loadNibNamed:@"WidgetView" owner:self options:nil]; 
    [self addSubview:self.view]; 
}