IOS_6_ToolBar + xib + Dream of Red Mansions, ios_6_toolbarxib

Source: Internet
Author: User

IOS_6_ToolBar + xib + Dream of Red Mansions, ios_6_toolbarxib

Final




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; // It is dragged from the xib interface, provided that the file's owner in the interface is set as the current controller class and when nib is loaded in the code, you must also specify that the owner is the Instance Object of the current controller class. Generally, write self or null-(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 // class extension is also called anonymous classification @ interface BeyondViewController () {// member, an array consisting of NSArray * _ array_name; // index when the array value is set, which is linked to the image name: int _ index;} @ end @ implementation BeyondViewController-(void) viewDidLoad {[super ViewDidLoad]; _ array_name = @ [@ "Lin Daiyu", @ "Xue Baochai", @ "Miao Yu", @ "Shi xiangcloud", @ "Chun", @ "Qing Wen ", @ "nana"]; _ index = 0;}-(IBAction) addClick :( UIBarButtonItem *) sender {// call a custom method, use the code to create a row rowView // UIView * rowView = [self createRowViewByCoding]; // call the custom method and use xib to create a row rowView UIView * rowView = [self createRowViewByXcodeInterfaceBuilding]; // call a custom method and return a RowView instance object through the RowView class method. // UIView * rowView = [self createRowViewByXIB];/ /3, add to 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 users to directly repair the value of the members of the struct attribute in this object, the temporary struct variable CGRect frame = rowView must be passed. frame; frame. origin. x = 0; rowView. frame = frame; rowView. alpha = 1 ;}completion: ^ (BOOL finished) {// 4, set the delete button to available _ trashItem. enabled = YES;}];} // use xib to create a rowView, xib <--> nib ipa <--> apk-(UIView *) createRowViewByXI B {// if it is added to the last array, add if (_ index> = _ array_name.count) {_ index = 0 ;}// the following three parameters first, image name, name, and Y coordinate NSString * imgName = [NSString stringWithFormat: @ "symbol d.png", _ index] of the frame to be displayed in rowView; NSString * labelName = _ array_name [_ index]; // The y value of the new row is determined by the y + height + 1 UIView * lastView = self of the last child control in the view. view. subviews. lastObject; CGFloat rowY = lastView. frame. origin. y + lastView. frame. size. height + 1; // call the class method and return A created rowView RowView * rowView = [RowView rowViewWithHeadName: imgName andLabelName: labelName andRowY: rowY]; // prepares for the next addition of rows _ index ++; return rowView ;} // use xib to create a row of rowView, xib <--> nib ipa <--> apk-(UIView *) createRowViewByXcodeInterfaceBuilding {// mainBundel to load xib. The extension does not need to be written. when the xib owner is nil, manually addTarget is used. If it is the Instance Object of the class specified by the file's owner in the xib interface, fill in self. In this way, you can directly drag/1, the class specified by the file's owner in the xib interface is used only when you right-click the interface, a line is displayed/ /2, connect. // 3. Specify the Instance Object of the owner in the Code loadNibNamed, which is equivalent to the first parameter NSArray * arrayXibObjects = [[NSBundle mainBundle] loadNibNamed: @ "rowView" owner: self options: nil]; UIView * rowView = arrayXibObjects [0]; // The y value of the new row is determined by the y + height + 1 UIView * lastView = self of the last child control in the view. view. subviews. lastObject; 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 (320, rowY, winWidth, kRowHight); rowView. alpha = 0; // if it is added to the last array, add if (_ index> = _ array_name.count) {_ index = 0 ;}// 2, set the label content UILabel * name = (UILabel *) [rowView viewWithTag: 1]; name. text = _ array_name [_ index]; // 3. Set the headBtn content UIButton * btn = (UIButton *) [rowView viewWithTag: 2]; NSString * imgName = [NSString stringWithFormat: @ "cmdd.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 :) forC OntrolEvents: UIControlEventTouchUpInside]; // prepare for the next addition of rows _ index ++; return rowView;} // create a row rowView-(UIView *) using code *) createRowViewByCoding {// if it is added to the last array, add if (_ index >=_ array_name.count) {_ index = 0;} // to add a row, which is actually a view, the picture on the left and UIView * rowView = [[UIView alloc] init] on the right. // The y value of the new row is determined by the y + height + 1 UIView * lastView = self of the last child control in the view. view. subviews. lastObject; CGFloat rowY = lastView. fra Me. origin. y + lastView. frame. size. height + 1; rowView. backgroundColor = [UIColor grayColor]; CGFloat winWidth = self. view. frame. size. width; rowView. frame = CGRectMake (320, rowY, winWidth, kRowHight); rowView. alpha = 0; // 2, add label to view UILabel * name = [[UILabel alloc] init]; name. frame = CGRectMake (0, 0,320, kRowHight); name. backgroundColor = [UIColor clearColor]; name. textAlignment = NSTextAlignmen TCenter; name. tag = 1; // when you click the Avatar button next to it, you can obtain the sibling tag, that is, name // Random Index and name, // int randIndex = arc4random_uniform (_ array_name.count); // name. text = _ array_name [randIndex]; name. text = _ array_name [_ index]; [rowView addSubview: name]; // 3. Add the Avatar to view // UIImage * img = [UIImage imageNamed: @ "nana.jpg"]; // UIImageView * head = [[UIImageView alloc] initWithImage: img]; // head. frame = CGRectMake (0, 0, 50, 50); // [rowView DdSubview: head]; // 3. Add the Avatar button to view UIButton * btn = [[UIButton alloc] init]; btn. frame = CGRectMake (0, 0, 65, kRowHight); NSString * imgName = [NSString stringWithFormat: @ "mongod.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 the delete button to view UIButton * del = [[UIButton alloc] init]; del. frame = CGRectMake (260, 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 addition of rows _ index ++; return rowView ;} // click the delete button in rowView-(void) deleteBtnClick :( UI Button *) sender {// get rowView UIView * rowView = sender. superview; [UIView animateWithDuration: 0.3 animations: ^ {// the following three steps are the OC standard code, because OC does not allow users to directly repair the value of the members of the struct attribute in this object, the temporary struct variable CGRect frame = rowView to be passed. frame; frame. origin. x = 320; rowView. frame = frame; rowView. alpha = 0;} completion: ^ (BOOL finished) {// The index int rowView_index = [self. view. subviews indexOfObject: rowView]; // extract rowView from its parent control, that is, self. vi Delete [rowView removeFromSuperview]; _ trashItem. enabled = self. view. subviews. count! In ew! = 1; // The rowView animations behind the rowView move up for (int I = rowView_index; I <self. view. subviews. count; I ++) {// move these rowView animations up behind rowView * rowViewSibling = self. view. subviews [I]; [UIView animateWithDuration: 0.3 animations: ^ {// the following three steps are the OC standard code, because OC does not allow users to directly repair the value of the members of the struct attribute in this object, the temporary struct variable CGRect frame = rowViewSibling must be passed. frame; frame. origin. y-= kRowHight + 1; rowViewSibling. frame = frame;}] ;}}] ;}// click the Avatar button to bring up alterView -( Void) headBtnClick :( UIButton *) sender {NSLog (@ "click the Avatar button -- % @", sender); UIView * rowView = sender. superview; UILabel * name = (UILabel *) [rowView viewWithTag: 1]; NSLog (@ "the sibling tag of the clicked button is: % @", name ); // pop up alterView UIAlertView * alert = [[UIAlertView alloc] init]; [alert show] ;}// click the delete button Item-(IBAction) removeClick :( UIBarButtonItem *) sender {_ trashItem. enabled = NO; // Delete the last row of 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 the OC standard code, because OC does not allow users to directly repair the value of the members of the struct attribute in this object, the temporary struct variable CGRect frame = last. frame; frame. origin. x = 320; last. frame = frame; last. alpha = 0;} completion: ^ (BOOL finished) {// After the animation is completed, remove [last removeFromSuperview] from the parent control; // After the animation is deleted, let the deleted barbuttionitem be dimmed _ trashItem. enable D = 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 you perform custom drawing. // An empty implementation adversely affects performance during animation. -(void) drawRect :( CGRect) rect {// Drawing code} * // you cannot directly use _ to access member variables in class methods. You must use objects. point syntax access member variable // parameter 1: icon name of the Avatar button, parameter 2: name label + (RowView *) rowViewWithHeadName :( NSString *) headName andLabelName :( NSString *) labelName andRowY :( CGFloat) rowY {// mainBundel loads xib, no need to write the extension. xib NSArray * arrayXibObjects = [[NSBundle mainBundle] loadNibNamed: @ "rowView" owner: nil options: nil]; RowView * rowView = arrayXibObjects [0]; // 1, sets the rowView attribute. backgroundColor = [UIColor grayColor]; // It is outside the screen, so x is 320 rowView. frame = CGRectMake (320, rowY, 320, kRowHight); rowView. alpha = 0; // 2. Set the label content. // The tag must be traversed, Which is inefficient and not recommended. It is best to use rowView. xib is connected to RowView. h file, use the member variable // UILabel * nameLabel = (UILabel *) [rowView viewWithTag: 1]; rowView. nameLabel. text = labelName; // 3. Set headBtn content // The tag to be traversed, Which is inefficient and not recommended, preferably rowView. xib is connected to RowView. h file, use the member variable // UIButton * headBtn = (UIButton *) [rowView viewWithTag: 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 the OC standard code, because OC does not allow users to directly repair the value of the members of the struct attribute in this object, the temporary struct variable CGRect frame = rowView to be passed. frame; frame. origin. x = 320; rowView. frame = frame; rowView. alpha = 0;} completion: ^ (BOOL finished) {// NSLog (@ "rowView. superView is % @ ", rowView. superview); // obtain the Controller's UIView * control_view = rowView. superview; // The index int rowView_index = [control_view.subviews indexOfObject: rowView] of rowView in the parent container; // extract rowView from its parent control, that is, self. delete [rowView removeFromSuperview]; // move these rowView animations up behind rowView (int I = rowView_index; I <control_view.subviews.count; I ++) {// The rowView animations behind the rowView 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 users to directly repair the value of the members of the struct attribute in this object, the temporary struct variable CGRect frame = rowViewSibling must be passed. frame; frame. origin. y-= kRowHight + 1; rowViewSibling. frame = frame;}] ;}}] ;}@ end


RowView. xib




















In iOS development, an exception occurs when XIB is enabled.

He said that the selector could not be found. Have you deleted the controller file connected to the xib by mistake? Or you mistakenly assigned the xib to other controllers when modifying the xib attribute...
No. Just re-create an xib. Maybe not all xib is hard to do... right?

How can I visually edit a UIView for ios development? Why must the uiview in the xib file be full screen?

UIVIew can be adjusted in xib. You can set the Attribute Editor on the right side of xcode.
 

Related Article

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.