问题
I have a iOS 5 project nearly completed, but there's one View missing everywhere which is the same. It's an alternative to the standard NavigationBar, you can go "back" in the navigation hierarchy with a swipe and it's animating nicely.
Ok, because it's hard to do the layout by code I created the empty IB document (HeaderView.xib) where I have a view containing subview and etc. I had the animation code before so I just created a UIView subclass ("HRAnimationView") (and wrote the name of it in the xib's inspectors "Custom Class" field, also hooked up the subviews to outlets) with a method:
- (void)loadAnimation {…}
and a second one (this is the delegate-method for finished animations):
- (void)animationDidStop:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {}
...this is where all the animation stuff happens and calling itself over and over till it's finished...
In the storyboard I have a subview with exact the same dimensions (and the outlet for it) and wanted to load the XIB (in the viewDidLoad-method of the corresponding controller) with:
NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil];
HRAnimationView *view;
for (id object in bundle) {
NSLog(@"%@",object);
if ([object isKindOfClass:[UIView class]])
view = (HRAnimationView *)object;
}
self.headerView = view;
[self.view setNeedsDisplay];
[view loadAnimation];
BUT the headerView is EMTPY!! (also UIView *view didn't worked, nor owner:self.headerView)
…the Log just gives me:
<HRAnimationView: 0x3f1890; frame = (0 0; 240 49); autoresize = RM+BM; layer = <CALayer: 0x3f18d0>>
…
WHAT IS the problem?? There's also no compilation failure!
I don't get it, why is the xib TOTALLY worthless in my case?!
回答1:
NSArray *bundle = [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil];
after the above statement... try adding this
[self.headerView = [bundle objectAtIndex:0]
this should solve the problem
回答2:
SOLVED!!!
I created a new ViewController and checked the "create with xib" box and deleted the view of the xib and copy&pasted the already set up view in there and in the ViewController I put the animation code in viewDidLoad.
Then loaded the hole thing in the viewDidLoad-method of each storyboard ViewController with:
UIViewController *ctrl = [[UIViewController alloc] initwithNibNamed:@"HeaderView"];
[self.headerView addSubview:ctrl.view]
(headerView is a IBOutlet to a view in every Storyboard so, that's basically the only thing to setup, NICE!)
- another possibility would be: Add subview using storyboard without external xib
- or: http://forums.macrumors.com/showthread.php?t=749544
Looks like a Xib is useless without a corresponding ViewController, but should (strange.. whatever). But thanks for your responses, hope someone now can find this if he/she has the same problem!
来源:https://stackoverflow.com/questions/10450577/ios-a-nibs-view-used-across-storyboards-for-design-animation-logic