objective c - Two switches in a window on iPhone -
i've started learning how develop iphone app.
i'm trying make app 2 switches. made 2 classes (switch1 & switch2). first, tested app 1 switch (switch1), , app worked. when made second class (switch2) , build/run app, first switch (switch1) disappeared, , saw second switch (switch2).
after made background of (switch1 & switch2) celarcolor, see both of switches. however, first switch (switch1) can't switched.
so think problem how make both switches (switch1 & switch2) visible , working @ same time in "window"
the question (could stupid): can make them visible , working @ same time? think problem in following code: appdelegate
uiscreen *s1 = [uiscreen mainscreen]; view1 = [[switch1 alloc] initwithframe: s1.applicationframe]; window = [[uiwindow alloc] initwithframe: s1.bounds]; [window addsubview: view1]; [window makekeyandvisible]; uiscreen *s2 = [uiscreen mainscreen]; view2 = [[switch2 alloc] initwithframe: s2.applicationframe]; window = [[uiwindow alloc] initwithframe: s2.bounds]; [window addsubview: view2]; [window makekeyandvisible]; return yes;
here switch1.h #import
@interface switch1 : uiview { uiswitch *myswitch1; } @property (nonatomic, retain) iboutlet uiswitch *myswitch1; @end
here switch1.m
#import "switch1.h" @implementation switch1 @synthesize myswitch1; - (id) initwithframe: (cgrect) frame { if ((self = [super initwithframe: frame])) { // initialization code self.backgroundcolor = [uicolor clearcolor]; myswitch1 = [[uiswitch alloc] initwithframe: cgrectzero]; if (myswitch1 == nil) { [self release]; return nil; } myswitch1.on = no; //the default [myswitch1 addtarget: [uiapplication sharedapplication].delegate action: @selector(valuechanged:) forcontrolevents: uicontroleventvaluechanged ]; cgrect b1 = self.bounds; myswitch1.transform = cgaffinetransformmakescale(2, 2); myswitch1.center = cgpointmake( b1.origin.x + b1.size.width / 2, b1.origin.y + b1.size.height / 2 ); [self addsubview: myswitch1]; } return self; } /* // override drawrect: if perform custom drawing. // empty implementation adversely affects performance during animation. - (void) drawrect: (cgrect) rect { // drawing code } */ - (void) dealloc { [myswitch1 release]; [super dealloc]; } @end
you might want configure view controller view , toss 2 switches on first. understand mvc patterns here: https://developer.apple.com/library/ios/#documentation/general/conceptual/devpedia-cocoacore/mvc.html
and here's view controller guide: https://developer.apple.com/library/ios/#featuredarticles/viewcontrollerpgforiphoneos/introduction/introduction.html.
when you're initializing uiscreen, you're making both switches same size (the window size) , switch 2 on switch 1 since it's initialized second.
Comments
Post a Comment