The effect is as follows:
ViewController.h
1 #import <UIKit/UIKit.h>23@interface viewcontroller: Uiviewcontroller4 @property (Strong, Nonatomic) Nsarray *arrcolorname; 5 6 @end
Viewcontroller.m
1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4- (void) Layoutui;5- (void) Segmentdidchange: (Uisegmentedcontrol *) sender;6 @end7 8 @implementationViewcontroller9 Ten- (void) Viewdidload { One [Super Viewdidload]; A - [self layoutui]; - } the -- (void) didreceivememorywarning { - [Super didreceivememorywarning]; - //Dispose of any resources the can be recreated. + } - +- (void) Layoutui { A_arrcolorname = @[@"Blackcolor",@"Bluecolor",@"Browncolor"]; atUicolor *color = [Uicolor colorwithred:1.000Green0.473Blue0.813Alpha1.000]; -Self.view.backgroundColor =color; - -Uisegmentedcontrol *smtbackgroundcolor =[[Uisegmentedcontrol alloc] initwithitems:_arrcolorname]; -Smtbackgroundcolor.frame = CGRectMake (0,0, -, +); -Smtbackgroundcolor.tintcolor =color; in [Smtbackgroundcolor addtarget:self - Action: @selector (segmentdidchange:) to forcontrolevents:uicontroleventvaluechanged]; + - //append a segment card to the right side of the navigation bar theUibarbuttonitem *barbtnright =[[Uibarbuttonitem alloc] initwithcustomview:smtbackgroundcolor]; *Self.navigationItem.rightBarButtonItem =Barbtnright; $ }Panax Notoginseng -- (void) Segmentdidchange: (Uisegmentedcontrol *) Sender { theNSString *colorname =_arrcolorname[sender.selectedsegmentindex]; +SEL Selcolor =nsselectorfromstring (colorname); AUicolor *color =[Uicolor Performselector:selcolor]; theSelf.view.backgroundColor =color; +Sender.tintcolor =color; - } $ $ @end
AppDelegate.h
1 #import <UIKit/UIKit.h>23@interface Appdelegate:uiresponder < Uiapplicationdelegate>4 @property (Strong, Nonatomic) UIWindow *window; 5 @property (Strong, Nonatomic) Uinavigationcontroller *Navigationcontroller; 6 7 @end
Appdelegate.m
1 #import "AppDelegate.h"2 #import "ViewController.h"3 4 @interfaceappdelegate ()5 @end6 7 @implementationappdelegate8 9-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {Ten_window =[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; OneViewcontroller *viewcontroller =[[Viewcontroller alloc] init]; A_navigationcontroller =[[Uinavigationcontroller alloc] initwithrootviewcontroller:viewcontroller]; -_window.rootviewcontroller =_navigationcontroller; - [_window Addsubview:_navigationcontroller.view]; the [_window makekeyandvisible]; - returnYES; - } - +- (void) Applicationwillresignactive: (UIApplication *) Application { - } + A- (void) Applicationdidenterbackground: (UIApplication *) Application { at } - -- (void) Applicationwillenterforeground: (UIApplication *) Application { - } - -- (void) Applicationdidbecomeactive: (UIApplication *) Application { in } - to- (void) Applicationwillterminate: (UIApplication *) Application { + } - the @end
070 Select a segment card to change the background color of the screen