Ios_6_toolbar+xib+ Dream of Red mansions

Source: Internet
Author: User

Eventually




BeyondViewController.h

  beyondviewcontroller.h//  6_toolbar////  Created by Beyond on 14-7-24.//  Copyright (c) 2014 Com.beyond. All rights reserved.//#import <UIKit/UIKit.h> @interface beyondviewcontroller:uiviewcontroller-( ibaction) AddClick: (Uibarbuttonitem *) sender;-(ibaction) Removeclick: (Uibarbuttonitem *) sender; @property (Weak, nonatomic) Iboutlet Uibarbuttonitem *trashitem;//is dragged from the Xib interface, provided that the file ' s owner is the current controller class in the Setup interface, and that when the nib is loaded in the code, Also indicate that owner is an instance object of the current controller class, generally write self or empty-(ibaction) Deletebtnclick: (UIButton *) sender;-(ibaction) Headbtnclick: (UIButton * ) Sender; @end


Beyondviewcontroller.m

beyondviewcontroller.m//6_toolbar////Created by Beyond on 14-7-24.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "BeyondViewController.h" #import "RowView.h" #define krowhight 65//Classes Extension class extension    Also known as anonymous category @interface Beyondviewcontroller () {///member, array, composed of names Nsarray *_array_name; The index of the array value, and the name of the image, int _index;}    @end @implementation beyondviewcontroller-(void) viewdidload{[Super Viewdidload]; _array_name [email protected][@ "Lin Daiyu", @ "Xue Bao Chai", @ "Miaoyu", @ "Shi Xiangyun", @ "Spring", @ "Ching Wen", @ "nana"];_index = 0;} -(Ibaction) AddClick: (Uibarbuttonitem *) Sender {//Call custom method, create a line through code Rowview//UIView *rowview = [Self Createrowview        Bycoding];        Call a custom method to create a line rowview UIView *rowview = [self createrowviewbyxcodeinterfacebuilding] through xib;        Call the custom method, returning a Rowview instance object through the Rowview class method//UIView *rowview = [self createrowviewbyxib];    3, add to the current controller view [Self.view Addsubview:rowview]; 5, animation effect [UIView animatewithduration:0.2 animations:^{// The following three steps are the OC Standard Code, because OC does not allow the value of the member of the struct attribute in the object to be repaired directly, through the intermediate temporary structure body variable cgrect frame = Rowview.frame;        frame.origin.x = 0;        Rowview.frame=frame;    Rowview.alpha = 1;    } completion:^ (BOOL finished) {//4, the Reset Delete button is available _trashitem.enabled = YES; }];} Create a line with Xib rowview,xib <--> nib IPA <-->apk-(UIView *) createrowviewbyxib{//If added to the last sheet of the array, add the if from the beginning (_index >= _array_name.count)    {_index = 0;     }//below first calculate 3 parameters, picture name, name, Rowview to display the frame's y-coordinate nsstring *imgname = [NSString stringwithformat:@ "%d.png", _index];    NSString *labelname = _array_name[_index];    The Y value of the newly added row depends on the y + height + 1 UIView *lastview = Self.view.subviews.lastObject of the last child control in the view;        CGFloat rowy = lastview.frame.origin.y + lastview.frame.size.height+1; Call the class method, return a created rowview rowview *rowview = [Rowview rowviewwithheadname:imgname andlabelname:labelname AndRowY:rowY]        ;    Prepare for the next add line _index++; return Rowview;} Create a line rowview,xib with Xib <--Nib IPA <-->apk-(UIView *) createrowviewbyxcodeinterfacebuilding{//Mainbundel load xib, extension not written. Xib owner for NI L, manual addtarget, if you are the instance object of class specified by the file ' s owner in the Xib interface, fill in self, so that you can drag the line directly//1,xib the class of file ' owner specified in the interface, for the purpose of just right-clicking the interface. Can eject the line//2, to connect//3, the code loadnibnamed specifies which instance object owner is, equivalent to the first parameter in addtarget nsarray *arrayxibobjects = [NSBundle mai    Nbundle] loadnibnamed:@ "Rowview" owner:self Options:nil];    UIView *rowview = arrayxibobjects[0];    The Y value of the newly added row depends on the y + height + 1 UIView *lastview = Self.view.subviews.lastObject of the last child control in the view;    CGFloat rowy = lastview.frame.origin.y + lastview.frame.size.height+1;    Rowview.backgroundcolor = [Uicolor Graycolor];    CGFloat winwidth = self.view.frame.size.width;    Rowview.frame = CGRectMake (Rowy,winwidth, krowhight);            Rowview.alpha = 0;    If added to the last sheet of the array, add the IF (_index >= _array_name.count) {_index = 0) from the beginning;        }//2, set label content UILabel *name = (UILabel *) [Rowview viewwithtag:1]; Name. Text = _array_name[_index];    3, set headbtn content UIButton *btn = (UIButton *) [Rowview viewwithtag:2];    NSString *imgname = [NSString stringwithformat:@ "%d.png", _index];    UIImage *img = [UIImage imagenamed:imgname];    [Btn setimage:img Forstate:uicontrolstatenormal];        Add a Click event for the button//[BTN addtarget:self Action: @selector (Headbtnclick:) forcontrolevents:uicontroleventtouchupinside];    4, set deletebtn content UIButton *del = (UIButton *) [Rowview Viewwithtag:3];        Add a click event for the Delete button [Del addtarget:self action: @selector (Deletebtnclick:) forcontrolevents:uicontroleventtouchupinside];    Prepare for the next add line _index++; return Rowview;}        Create a line with code rowview-(UIView *) createrowviewbycoding{//If added to the last sheet of the array, add the IF (_index >= _array_name.count) from the beginning {    _index = 0;    }//Add a row, real view,view in the left is the avatar, the right is the name UIView *rowview = [[UIView alloc]init];    The Y value of the newly added row depends on the y + height + 1 UIView *lastview = Self.view.subviews.lastObject of the last child control in the view; CGFloat rowy = LastVIEW.FRAME.ORIGIN.Y + lastview.frame.size.height+1;    Rowview.backgroundcolor = [Uicolor Graycolor];    CGFloat winwidth = self.view.frame.size.width;    Rowview.frame = CGRectMake (Rowy,winwidth, krowhight);    Rowview.alpha = 0;    2, add label to view UILabel *name = [[UILabel alloc]init];    Name.frame = CGRectMake (0, 0, +, krowhight);    Name.backgroundcolor = [Uicolor Clearcolor];    Name.textalignment = Nstextalignmentcenter; Name.tag = 1;    Convenient back click on the Avatar button, get the brother tag that name//random index, take the name, take the picture with the//int Randindex = Arc4random_uniform (_array_name.count);    Name.text = _array_name[randindex];    Name.text = _array_name[_index];    [Rowview Addsubview:name]; 3, add avatar to view//UIImage *img = [UIImage imagenamed:@ "nana.jpg"];//uiimageview *head = [[Uiimageview ALLOC]INITW        ithimage:img];//head.frame = CGRectMake (0, 0,50, +);//[Rowview Addsubview:head];    3, add Avatar button to view UIButton *btn = [[UIButton alloc]init]; Btn.frame = CGRectMake (0, 0,65, krowhight);    NSString *imgname = [NSString stringwithformat:@ "%d.png", _index];    UIImage *img = [UIImage imagenamed:imgname];    [Btn setimage:img Forstate:uicontrolstatenormal];    Add a Click event for the button [BTN addtarget:self action: @selector (Headbtnclick:) forcontrolevents:uicontroleventtouchupinside];        [Rowview ADDSUBVIEW:BTN];    4, add Delete button to view UIButton *del = [[UIButton alloc]init];    Del.frame = CGRectMake (260, 0,65, krowhight);    [Del settitle:@ "Goodbye" forstate:uicontrolstatenormal];    Add a click event for the Delete button [Del addtarget:self action: @selector (Deletebtnclick:) forcontrolevents:uicontroleventtouchupinside];        [Rowview Addsubview:del];    Prepare for the next add line _index++; return Rowview;}    Click the Delete button inside the Rowview-(void) Deletebtnclick: (UIButton *) sender{//Get rowview UIView *rowview = Sender.superview; [UIView animatewithduration:0.3 animations:^{///The following three steps are OC Standard Code, because OC does not allow directly to repair the value of the member of the struct property in the object, to pass the intermediate temporary structure body variable CGRect        frame = Rowview.frame;        Frame.origin.x = 320; RowView.frame=frame;    Rowview.alpha = 0; } completion:^ (BOOL finished) {//Rowview in parent container index int rowview_index = [Self.view.subviews indexofobject:row        View];         Remove Rowview from its parent control, or Self.view [Rowview Removefromsuperview];                _trashitem.enabled = self.view.subviews.count!=1; Rowview behind these Rowview animations move up for (int i=rowview_index; i<self.view.subviews.count; i++) {//Rowview behind            These rowview animations move up UIView *rowviewsibling = self.view.subviews[i];                [UIView animatewithduration:0.3 animations:^{//The following three steps are OC Standard Code, because OC does not allow directly to repair the value of the member of the struct property in the object, to pass the intermediate temporary structure body variable                CGRect frame = rowviewsibling.frame;                FRAME.ORIGIN.Y-= krowhight + 1;            Rowviewsibling.frame=frame;        }];    }    }];    }//Click on the Avatar button, pop-up alterview-(void) Headbtnclick: (UIButton *) sender{NSLog (@ "Click the Avatar button--%@", sender);    UIView *rowview = Sender.superview; UILabel *name = (UILabel *) [Rowview VIEWWITHTAG:1];        NSLog (@ "The brother tag of the clicked button is:%@", name);        Eject Alterview Uialertview *alert = [[Uialertview alloc]init]; [Alert show];}    Click the Delete button item-(ibaction) Removeclick: (Uibarbuttonitem *) Sender {_trashitem.enabled = NO;    Delete last line UIView *last = [Self.view.subviews lastobject];    Class CLS = [Uitoolbar class];    if ([last Iskindofclass:cls]) {return; }//animation effect [UIView animatewithduration:0.2 animations:^{///The following three steps are OC Standard Code, because OC does not allow directly to repair the value of the member of the struct attribute in the object, to be changed through the intermediate temporary structure        Volume CGRect frame = last.frame;        Frame.origin.x = 320;        Last.frame=frame;    Last.alpha = 0;        } completion:^ (BOOL finished) {///animation finished, remove [last Removefromsuperview] from parent control;    After deletion, let's delete barbuttionitem gray _trashitem.enabled = self.view.subviews.count! = 1;        }]; } @end


RowView.h

  rowview.h//  6_toolbar////  Created by Beyond on 14-7-24.//  Copyright (c) 2014 Com.beyond. All rights reserved.//#import <UIKit/UIKit.h> @interface rowview:uiview@property (weak, nonatomic) Iboutlet UIButton *headbtn; @property (weak, nonatomic) Iboutlet UILabel *namelabel;-(ibaction) Deletebtnclick: (UIButton *) sender;+ (Rowview *) Rowviewwithheadname: (NSString *) headname andlabelname: (NSString *) labelname AndRowY: (CGFloat) Rowy; @end


rowview.m

rowview.m//6_toolbar////Created by Beyond on 14-7-24.//Copyright (c) 2014 Com.beyond. All rights reserved.//#import "RowView.h" #define Krowhight 65@implementation rowview-(ID) initWithFrame: (CGRect) frame    {self = [super Initwithframe:frame]; if (self) {//initialization code} return self;}  /*//only override drawrect:if your perform custom drawing.//an empty implementation adversely affects performance during animation.-(void) DrawRect: (cgrect) rect{//Drawing code}*///+ class method cannot access member variables directly, must pass object. Point syntax Access member variables//parameter 1: Icon name for Avatar button, parameter 2    : Name tag + (Rowview *) Rowviewwithheadname: (NSString *) headname andlabelname: (NSString *) labelname androwy: (CGFloat) rowY{ Mainbundel load xib, extension does not write. xib nsarray *arrayxibobjects = [[NSBundle mainbundle] loadnibnamed:@ "Rowview" Owner:nil optio        Ns:nil];    Rowview *rowview = arrayxibobjects[0];    1, set the properties of Rowview Rowview.backgroundcolor = [Uicolor Graycolor]; First on the outside of the screen, so x is the Rowview.frame = CGRectMake (rowy,320, Krowhight);        Rowview.alpha = 0; 2, set the label content//tag to traverse, inefficient, not recommended, preferably rowview.xib wire to RowView.h file, use member variable//UILabel *namelabel = (UILabel *) [Rowview vie        WWITHTAG:1];        RowView.nameLabel.text = LabelName; 3, set HEADBTN content//tag to traverse, inefficient, not recommended, preferably rowview.xib wire to RowView.h file, using member variable//UIButton *HEADBTN = (UIButton *) [Rowview V        Iewwithtag:2];    UIImage *img = [UIImage imagenamed:headname];    [Rowview.headbtn setimage:img Forstate:uicontrolstatenormal]; return Rowview;}         The Delete button in Rowview is clicked-(ibaction) Deletebtnclick: (UIButton *) Sender {//get rowview UIView *rowview = Sender.superview;        [UIView animatewithduration:0.3 animations:^{//The following three steps are OC Standard Code, because OC does not allow directly to repair the value of the member of the struct property in the object, to pass the intermediate temporary structure body variable        CGRect frame = rowview.frame;        Frame.origin.x = 320;        Rowview.frame=frame;    Rowview.alpha = 0;        } completion:^ (BOOL finished) {//nslog (@ "Rowview.superview is%@", Rowview.superview); First get the controller UIView UIView *contrOl_view = Rowview.superview;        Rowview index in the parent container int rowview_index = [Control_view.subviews Indexofobject:rowview];        Remove Rowview from its parent control, or Self.view [Rowview Removefromsuperview]; Rowview behind these Rowview animations move on for (int i=rowview_index; i<control_view.subviews.count; i++) {//Rowview            Behind these rowview animations move up UIView *rowviewsibling = control_view.subviews[i];                [UIView animatewithduration:0.3 animations:^{//The following three steps are OC Standard Code, because OC does not allow directly to repair the value of the member of the struct property in the object, to pass the intermediate temporary structure body variable                CGRect frame = rowviewsibling.frame;                FRAME.ORIGIN.Y-= krowhight + 1;            Rowviewsibling.frame=frame;        }]; }    }];} @end


Rowview.xib



















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.