objective c - Is this a correct approach to connect screens? -
i .net programmer , last week started read objective-c. class related stuff kinda clear , today learnt protocols , delegates, can't 100% clear got it, looks lot delegates , events c#.
this simple example created following tutorial. 2 screens, first one(a label , button) launches second one(a textbox , button) sends string. think of classic example of using events, no matter programming language.
#import <uikit/uikit.h> #import "valueviewcontroller.h" @interface viewcontroller : uiviewcontroller<valueviewcontrollerdelegate> - (ibaction)btngetvalue:(id)sender; @property (weak, nonatomic) iboutlet uilabel *lblcurrentvalue; @end #import "viewcontroller.h" #import "valueviewcontroller.h" @interface viewcontroller () @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; // additional setup after loading view, typically nib. } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } - (ibaction)btngetvalue:(id)sender { valueviewcontroller *valuevc = [self.storyboard instantiateviewcontrollerwithidentifier:@"valueviewcontroller"]; valuevc.delegate=self; [self presentviewcontroller:valuevc animated:false completion:nil]; } -(void) sendvalue:(valueviewcontroller *)controller didfihishwithvalue:(nsstring *)value { self.lblcurrentvalue.text=value; } @end #import <uikit/uikit.h> @class valueviewcontroller; @protocol valueviewcontrollerdelegate<nsobject> -(void) sendvalue:(valueviewcontroller*) controller didfihishwithvalue:(nsstring*) value; @end @interface valueviewcontroller : uiviewcontroller<uitextfielddelegate> @property (weak, nonatomic) iboutlet uitextfield *txtvalue; - (ibaction)btnsetvalue:(id)sender; @property (weak, nonatomic) id<valueviewcontrollerdelegate> delegate; @end #import "valueviewcontroller.h" @interface valueviewcontroller () @end @implementation valueviewcontroller - (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil { self = [super initwithnibname:nibnameornil bundle:nibbundleornil]; if (self) { // custom initialization } return self; } - (void)viewdidload { [super viewdidload]; self.txtvalue.delegate=self; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of resources can recreated. } -(bool)textfieldshouldreturn:(uitextfield *)textfield { return [textfield resignfirstresponder]; } - (ibaction)btnsetvalue:(id)sender { [self.delegate sendvalue:self didfihishwithvalue:self.txtvalue.text]; [self dismissviewcontrolleranimated:false completion:nil]; } @end
my question following: considering a, let's say, 30 screens application, allows sending , receiving messages, adding friends , etc approach group 4-5 message view controller storyboard, friends related view controllers storyboard , make connection did in simple example, programmatically?
i saw connections can done in designer without writing code, think have write code send arguments means mixing two(graphically , programmatically). feel more comfortable, doing programatically, maybe because how in c#.
i looking forward tips regarding organizing , making connections between screens.
ps: sorry writing such long story(board) in here, promise make shorter in following posts.
thanks.
making 2 storyboards communicate each other go against intended flow, because storyboards not intended grouping parts of application. although app may have multiple storyboards, intention behind allowing multiple storyboards letting support different screen paradigms (i.e. iphone vs. ipad) or different localizations, not grouping related screens together.
note, however, storyboards relatively new. can define views in nib files, , use them instead. unfortunate consequence of choice need make connections programmatically, on other hand able group views inside xcode project using file groups or folders.
Comments
Post a Comment