Thursday, 15 May 2014

ios - How do you hook up a custom UIView? -



ios - How do you hook up a custom UIView? -

writing first iphone app through tutorial--i have custom uiview method showdie , have 2 uiview elements on storyboard i've hooked viewcontroller. however, both uiview elements have error message "no visible @interface 'uiview' declares selector 'showdie'. think that's because uiviews aren't subclassing xyzdieview, when control-clicked storyboard, uiview appeared in dropdown--no xyzdieview. tried changing text manually in viewcontroller.h, didn't work (i didn't think would). on right track? how utilize subclass? (xcode 5)

xyzdieview.m

-(void)showdie:(int)num { if (self.dieimage == nil){ self.dieimage = [[uiimageview alloc] initwithframe:cgrectmake(0, 0, 90, 90)]; [self addsubview:self.dieimage]; } nsstring *filename = [nsstring stringwithformat:@"dice%d.png", num]; self.dieimage.image = [uiimage imagenamed:filename]; }

xyzviewcontroller.h

#import <uikit/uikit.h> #import "xyzdieview.h" @interface xyzviewcontroller : uiviewcontroller @property (weak, nonatomic) iboutlet uilabel *sumlabel; @property (strong, nonatomic) iboutlet uibutton *rollbutton; @property (strong, nonatomic) iboutlet uiview *leftdieview; @property (strong, nonatomic) iboutlet uiview *rightdieview; @end

xyzviewcontroller.m

#import "xyzviewcontroller.h" #import "xyzdicedatacontroller.h" @interface xyzviewcontroller () @end @implementation xyzviewcontroller - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (ibaction)rollbuttonclicked:(uibutton *)sender { xyzdicedatacontroller *dicedatacontroller = [[xyzdicedatacontroller alloc] init]; int roll = [dicedatacontroller getdiceroll]; int roll2 = [dicedatacontroller getdiceroll]; [self.leftdieview showdie:roll]; [self.rightdieview showdie:roll2]; int sum = roll + roll2; nsstring *sumtext = [nsstring stringwithformat:@"sum %i", sum]; self.sumlabel.text = sumtext; } @end

yes you're right, problem subclassing. in order have view conform subclass in interface builder need

click on uiview in storyboard open right inspector panel click on 3rd tab identity inspector add class

then seek connect viewcontroller again.

edit: afterwards may need cast xyzdieview myview = (xyzdieview*)leftdieview;

ios iphone objective-c cocoa-touch uiview

No comments:

Post a Comment