IOS Study Notes 36 -- UIPasteboard/UIMenuController (2)

Source: Internet
Author: User

Okay. In the previous article, we talked about generating a clipboard on UILabel. In this article, we will write two ways to use the clipboard on UITableView;

I. Use in custom Cells

In fact, cell usage is basically the same as label usage.

1. Release Method:


-(BOOL) canBecomeFirstResponder {

Return YES;

}

-(BOOL) can1_maction :( SEL) action withSender :( id) sender {

If (action ==@ selector (cut :)){

Return NO;

}

Else if (action ==@ selector (copy :)){

Return YES;

}

Else if (action ==@ selector (paste :)){

Return NO;

}

Else if (action ==@ selector (select :)){

Return NO;

}

Else if (action ==@ selector (selectAll :)){

Return NO;

}

Else {

Return [super canPerformAction: action withSender: sender];

}

}

Then, of course, the copy method is implemented ..

-(Void) copy :( id) sender {

NSLog (@ "copy ");

UIPasteboard * pasteboard = [UIPasteboard generalPasteboard];

[Pasteboard setString: [[self textLabel] text];

}

However, the most important step is to use this method to stimulate the copy menu.


-(Void) setHighlighted :( BOOL) highlighted animated :( BOOL) animated {

[[Self delegate] performSelector: @ selector (showMenu :)

WithObject: self afterDelay: 0.9f];

[Super setHighlighted: highlighted animated: animated];

}

As for the Menu Generation, you should have read the previous article and are familiar with the code:


// Display menu

-(Void) showMenu :( id) cell {

If ([cell isHighlighted]) {

[Cell becomeFirstResponder];

UIMenuController * menu = [UIMenuController sharedMenuController];

[Menu setTargetRect: [cell frame] inView: [self view];

[Menu setMenuVisible: YES animated: YES];

}

}

Okay, the rest is about how to use a custom cell. You should be familiar with it. I will not introduce it one by one. Since then, the first method of using clipboard has been introduced.
II. The second method is simpler, because (_ MAC_NA ,__ IPHONE_5_0) three methods have been released, which is very easy to use. I will directly use Farah above.


-(BOOL) tableView :( UITableView *) tableView shouldShowMenuForRowAtIndexPath :( NSIndexPath *) indexPath {

Return YES;

}

 


-(BOOL) tableView :( UITableView *) tableView can1_maction :( SEL) action forRowAtIndexPath :( NSIndexPath *) indexPath withSender :( id) sender {

If (action ==@ selector (copy :)){

Return YES;

}

Return NO;

}

 


-(Void) tableView :( UITableView *) tableView upload maction :( SEL) action forRowAtIndexPath :( NSIndexPath *) indexPath withSender :( id) sender {

UITableViewCell * cell = [tableView cellForRowAtIndexPath: indexPath];

If (action ==@ selector (copy :)){

[UIPasteboard generalPasteboard]. string = cell. textLabel. text;

}

}

With the above three methods, I think you can easily use the clipboard in the cell ..

Well, the second article is complete. Continue to focus on customizing the clipboard ----- see the next version.

 

 

 

 


 

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.