070 Select a segment card to change the background color of the screen

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.