iOS work notes (vi)

Source: Internet
Author: User

Description: Note that some of the trivia of learning iOS, some in the present seems very simple childish, but the right to learn footprints it!

1.The advantage of using Firstobject and Lastobject in Nsarray is that when the array is empty, nil is returned without error.

When the array is empty, using myarray[0] will cause an error.

Both 2.UILabel and Uiimageview can add click events, taking Uilabel as an example.

1 UITapGestureRecognizer *tapgesture = [[UITapGestureRecognizer alloc]initwithtarget:self action:@ Selector (Clickspecialwith)]; 2 // userinteractionenabled must be set to Yes, otherwise invalid 3 mylabel.userinteractionenabled = YES; 4 [MyLabel addgesturerecognizer:tapgesture];

3. In the normal view, some methods can be loaded in initwithframe, such as:

1 -(ID) initwithframe: (cgrect) frame{2Self     = [Super Initwithframe:frame ]; 3     if (self) {4         // Add selector 5         [self setupchoiceview]; 6     }7     return self ; 8 }

But in Tableviewcell, these methods are best written in Initwithstyle for easy reuse. such as

1 -(instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) Reuseidentifier2{3     if (self = [Super Initwithstyle:style Reuseidentifier:reuseidentifier]) {4         // custom method 5        [ Self setupftehitoryswitchbtn]; 6     }7     return self ; 8 }

4.view modal display, the effect is similar to the JSP's alert pop-up window, such as Click the Class A button, pop-up Class B interface, you can

A type of click Method is written in:

1 Uinavigationcontroller *navigationcontroller = [[Uinavigationcontroller alloc] Initwithrootviewcontroller:bcontroller]; 2 // set the mode of the pop-up window 3 navigationcontroller.modalpresentationstyle = uimodalpresentationformsheet; 4 [self Presentviewcontroller:navigationcontroller animated:yes completion:nil];

Then on the pop-up page, to return to Class A, you need to add the return method to the Class B.

1 //Set Back button2-(void) setupreturnbackbtn{3Uibarbuttonitem *backbarbtn = [[Uibarbuttonitem alloc]initwithtitle:@"Return"Style:uibarbuttonitemstyledone target:self Action: @selector (backtoprevious)];4 [backbarbtn settintcolor:[uicolor whitecolor];5     //define the bar to the left of the Baritem6Self.navigationItem.leftBarButtonItem =backbarbtn;7 }8 9 //back to previous pageTen-(void) backtoprevious{ One [self dismissviewcontrolleranimated:yes completion:nil]; A     //you can't use [self poptorootviewcontrolleranimated:yes] here . -}

5.UITableView refresh, divided into local refresh and all refresh.

1 // Refresh all cells in TableView 2  34// Refresh single cell, as below is the 1th cell5 Nsindexpath * That refreshes the 1th section Indexpath = [Nsindexpath indexpathforrow:0 insection:0]; 6 [Self.tableview Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationnone];

It is important to note that the subscript cannot be crossed.

6.respondsToSelector is used to determine if an object has a method, which is encapsulated in the Selector object, and is generally used in proxies.

In the cell, click the button to jump to another controller through the push method, only through the proxy, because there is no push this method in the cell.
In the cell's. h

1 @protocol Msfouthscatterdelegate <NSObject>2 -(void) Showfouthscatterview; 3 @end 4 5 @interface Msfouthscattercell:uitableviewcell 6 7 ID Delegate ; 8 9 @end

In the cell's. M,

[btn addtarget:self Action: @selector (CLICKSCATTERBTN) forcontrolevents:uicontroleventtouchupinside]; // It should be noted that the method names of the two places must be the same, otherwise error. 

-(void) clickscatterbtn{    if ([self].  Delegate  respondstoselector: @selector (Showfouthscatterview)]) {        [self. Delegate Showfouthscatterview];}    }

In the controller's. m

1 @interfaceMssalesfourthcontroller () <MSFouthScatterDelegate>2 3 @end4 @implementationMssalesfourthcontroller5 //jump to another controller6- (void) showfouthscatterview{7Msfouthscattercontroller *VC =[[Msfouthscattercontroller alloc] init];8 [Self.navigationcontroller PUSHVIEWCONTROLLER:VC animated:yes];9 }Ten @end

So you can.

7. A problem occurred while doing the login interface. When you add a Uitextfield that enters a user name and password to Uiimageview, TextField does not respond to the click, that is, the keyboard does not pop up.

At this point, just set imageview userinteractionenabled = YES.
This was done in the original process.

imageview.userinteractionenabled === = = YES;

But no effect, in fact, only need one line of code is enough, the rest can be deleted.

imageview.userinteractionenabled = YES;

8.tableView How do I not display a blank cell? As below, do not want to let the bottom blank display

Just one line of code

Self.tableView.tableFooterView = [[UIView alloc]init];

The results are as follows:

Of course, you can also use the trouble point method. In the controller

1 -(void) viewdidload{2    [Super Viewdidload]; 3 4     Self.tableView.separatorStyle = Uitableviewcellseparatorstylenone; 5 }

Then manually add the dashed line in the custom cell.

1- (void) Layoutsubviews2 {3 [Super Layoutsubviews];4     5     //Adding lines manually6UIView *view =[[UIView alloc] init];7View.frame = CGRectMake (Ten, Self.height-1, Self.width- -,1);8View.backgroundcolor =[Uicolor Lightgraycolor];9 [Self.contentview Addsubview:view];Ten}

9. How to change the color and position of Uitextfield placeholder?

1 -(void) Drawplaceholderinrect: (cgrect) rect {2    [[Uicolor Bluecolor] Setfill]; 3     [Self.placeholder drawinrect:rect withFont:self.font linebreakmode:uilinebreakmodetailtruncation Alignment:self.textAlignment]; 4 }

The Textlabel of 10.button is populated by default with the entire button, which can be determined by the following

Mybtn.backgroundcolor == [Uicolor Redcolor];

You can see that the whole button turns red.
When the button's background image looks a little bit, you can use the padding if you want the text to be placed inside the background image.
Equivalent to reducing the size of the Textlabel. By default, the padding for Textlabel is (0,0,0,0). Set padding can be used:

Mybtn.contentedgeinsets = Uiedgeinsetsmake (a);

The effect is that Textlabel is 20point from the top and bottom of the button respectively.

iOS work notes (vi)

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.