ios - loadNibNamed from storyboard -
this scenario: simple app 2 viewcontroller classes , storyboard 1 viewcontroller. viewcontroller 'super' inherits uiviewcontroller
, implements -loadview
, other view controller, 'sub', inherits 'super'. view controller class in storyboard 'sub'.
in code:
@implementation sosuperviewcontroller - (void)loadview { self.view = [[uiview alloc] initwithframe:[[uiscreen mainscreen] bounds]]; self.view.backgroundcolor = [uicolor redcolor]; nslog(@"%@=> %@", nsstringfromclass([self superclass]), nsstringfromselector(_cmd)); } @end @implementation sosubviewcontroller - (void)awakefromnib { //outlets lazy loaded self.view not set here [super awakefromnib]; nslog(@"%@=> %@", nsstringfromclass([self class]), nsstringfromselector(_cmd)); } @end
the class view controller in storyboard sosubviewcontroller
. when executing app can see -awakefromnib
invoked in 'sub' controller , later -loadview
invoked in 'super' controller , result screen red set in -loadview
in 'super'.
the first question is: there way avoid -loadview
in 'super' called letting 'sub' view storyboard?
if using xibs instead storyboards implement -loadview
in 'sub' view xib file, like:
- (void)loadview { self.view = [[[nsbundle mainbundle] loadnibnamed:@"soview" owner:self options:nil] firstobject]; }
but can't know name of nib file using storyboards, instead need uses nibbundle , nibname properties if set:
- (void)loadview { if (self.nibbundle && self.nibname) { [self.nibbundle loadnibnamed:self.nibname owner:self options:nil]; } }
but crashes:
2014-04-10 11:55:11.872 loadviewtest[8941:60b] *** terminating app due uncaught exception 'nsinternalinconsistencyexception', reason: 'could not load nib in bundle: 'nsbundle </users/nlg/library/application support/iphone simulator/7.1/applications/e36dfc3a-4222-4ce4-a954-cc1c3891dce1/loadviewtest.app> (loaded)' name 'vxz-lx-hvc-view-kh9-bi-dss'' *** first throw call stack: ( 0 corefoundation 0x017ec1e4 __exceptionpreprocess + 180 1 libobjc.a.dylib 0x0156b8e5 objc_exception_throw + 44 2 corefoundation 0x017ebfbb +[nsexception raise:format:] + 139 3 uikit 0x004e0b7b -[uinib instantiatewithowner:options:] + 951 4 uikit 0x004e2ada -[nsbundle(uinsbundleadditions) loadnibnamed:owner:options:] + 165 5 loadviewtest 0x00002cff -[sosuperviewcontroller loadview] + 319 6 uikit 0x0034a0d3 -[uiviewcontroller loadviewifrequired] + 78 7 uikit 0x0034a5d9 -[uiviewcontroller view] + 35 8 uikit 0x0026a267 -[uiwindow addrootviewcontrollerviewifpossible] + 66 9 uikit 0x0026a5ef -[uiwindow _sethidden:forced:] + 312 10 uikit 0x0026a86b -[uiwindow _orderfrontwithoutmakingkey] + 49 11 uikit 0x002753c8 -[uiwindow makekeyandvisible] + 65 12 uikit 0x00225bc0 -[uiapplication _callinitializationdelegatesforurl:payload:suspended:] + 2097 13 uikit 0x0022a667 -[uiapplication _runwithurl:payload:launchorientation:statusbarstyle:statusbarhidden:] + 824 14 uikit 0x0023ef92 -[uiapplication handleevent:withnewevent:] + 3517 15 uikit 0x0023f555 -[uiapplication sendevent:] + 85 16 uikit 0x0022c250 _uiapplicationhandleevent + 683 17 graphicsservices 0x037e1f02 _purpleeventcallback + 776 18 graphicsservices 0x037e1a0d purpleeventcallback + 46 19 corefoundation 0x01767ca5 __cfrunloop_is_calling_out_to_a_source1_perform_function__ + 53 20 corefoundation 0x017679db __cfrunloopdosource1 + 523 21 corefoundation 0x0179268c __cfrunlooprun + 2156 22 corefoundation 0x017919d3 cfrunlooprunspecific + 467 23 corefoundation 0x017917eb cfrunloopruninmode + 123 24 uikit 0x00229d9c -[uiapplication _run] + 840 25 uikit 0x0022bf9b uiapplicationmain + 1225 26 loadviewtest 0x0000309d main + 141 27 libdyld.dylib 0x01e33701 start + 1 ) libc++abi.dylib: terminating uncaught exception of type nsexception
the second question is: possible load nib using storyboards without xib file?
as curiosity, discovered value of self.nibname vxz-lx-hvc-view-kh9-bi-dss #object id viewcontroller in storyboard#-view-#object id view in viewcontroller# kind of right.
you can't.
loadnibname
is supposed load nib called soview.xib
in case.
if want load views, can create view in separate xib
files, when we're dealing storyboards, add uiviewcontroller
inside them (which of course, can contains custom views).
to instantiate method storybard use method :
[self.storyboard instantiateviewcontrollerwithidentifier:@"mycontrollerid"];
Comments
Post a Comment