objective c - Loading XIB file without a UIViewController -
i'd design uiview , sub-views (uiwebview, uitoolbar, uibarbuttonitems, progress indicator , so-forth) using interface builder, think it's unnecessary traditionally, using uiviewcontroller, using presentviewcontroller:animated
etc.
so, created custom class, .h
file code follows:
@interface fileinteractionmanager : nsobject { } @property (nonatomic, retain) iboutlet uiview *fileview; @property (nonatomic, retain) iboutlet uiwebview *filewebview; @property (nonatomic, retain) iboutlet uibarbuttonitem *printbutton; @property (nonatomic, retain) iboutlet uibarbuttonitem *optionsbutton; @property (nonatomic, retain) iboutlet uibarbuttonitem *donebutton;
my .m file follows:
@implementation fileinteractionmanager @synthesize fileview, filewebview, donebutton, optionsbutton, printbutton; -(id)init { nsarray *array = [[nsbundle mainbundle] loadnibnamed:@"fileinteractionview" owner:self options:nil]; nslog(@"load success!"); return self; }
finally, create stand-alone xib file named 'fileinteractionview.xib', change file's owner custom class created above, , wire iboutlets.
when call init
method on class, can see in debugger iboutlet objects instantiated properly.
my questions are:
is
loadnibnamed:owner:options:
method right way load stand-alone .xib file? don't fact method returns array have no use (the top-level object returned matches variablefileview
, i've linked them through interface builder).is general approach correct in solving problem? carried out above steps because wanted simple
uiview
object add existing uiviewcontroller, rather present , dismiss whole new uiviewcontroller.
i use little different approach. create subclass of uiview (mycustomview i.e.) xib ui of view , change (main) view class the 1 defined. in xib can link outlet custom view (not file owner). in class definition create function this:
+ (id) newfromnib { nsarray *nibarray = [[uinib nibwithnibname:nsstringfromclass([self class]) bundle:nil] instantiatewithowner:nil options:nil]; return nibarray[0]; }
just couple of notes: 1) this's class method, can use "self" stuff "nsstringfromclass([self class])" real object variable returned 2) example suppose xib have same name of class (via nsstringfromclass([self class]) can copy-paste without changing ;) ) , view first 1 defined in xib (the standard). if store more "main" view inside 1 xib pick right element.
so need mycustomview like:
mycustomview* mycv = [mycustomview newfromnib];
then set frame/center , add superview... think way pretty usefull if have "library" of complex ui elements , want design them via xib add when needed.
Comments
Post a Comment