IOS Protocol usage

Source: Internet
Author: User

IOS Protocol usage
This is a cell, an image placed in the box (there are many images that can slide left and right, I made it with iCarousel, this proxy is written in the Custom cell class ). Now you need to click the image to go to the next page .... Now, when I click an image, it will go to the proxy in the Custom cell class, but the cell class won't let me bring up the next viewcontroller ..?? Otherwise, use the [self. navigationController pushViewController: show animated: YES]; method.


Finally, use the protocol. When a user clicks an image, the iCarousel proxy method of the cell class-(void) carousel :( iCarousel *) carousel didSelectItemAtIndex :( NSInteger) index transmits the index to StarShowViewController, then perform the push action in StarShowViewController.

(1) define the Protocol ImageItemDelegate. h # import

@ Protocol ImageItemDelegate

-(Void) passItem :( NSString *) index; // upload the index of the image you clicked

@ End



(2) follow the protocol in the StarShowViewController. h class
# Import "ImageItemDelegate. h"
@ Interface StarShowViewController: UIViewController {

UITableView * starTableView;
NSMutableArray * starArray;


}



(4) declare a proxy in the StarShowTableViewCell. h class
# Import "ImageItemDelegate. h"

@ Interface StarShowTableViewCell: UITableViewCell

@ Property (nonatomic, retain) NSObject * ItemDelegate; // declare a proxy


(5) Pass the value in the StarShowTableViewCell. m class

// Select an image item
-(Void) carousel :( iCarousel *) carousel didSelectItemAtIndex :( NSInteger) index {

NSLog (@ "index: % ld", (long) index );
NSString * str = [NSString stringWithFormat: @ "% ld", index];


UIView * view = carousel. currentItemView;

[Self. itemDelegate passItem: str]; // transmits str to the StarShowViewController class through a proxy.


}


(6) proxy method of the table in StarShowViewController. m-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath setting proxy
Cell. itemDelegate = self; // sets the proxy.


(7) Go to the next viewcontroller # pragma mark-ImageItemDelegate
// Get the value from the cell side
-(Void) passItem :( NSString *) index {

ShowViewController * show = [[ShowViewController alloc] init];
Show. imageIdex = index;
[Self. navigationController pushViewController: show animated: YES];

}



----------------------
Ask you to read books well, but not read books well! There are many implementation methods: ①. You can directly add click events to the Image in the controller. ②. You can use delegate to trigger click events in the current controller. ③ block simple. ④ event responder chains can be processed.
The first method is not suitable here. The third method has not been used yet. Theoretically, the block can also be used to transmit values. Considering that there are many block restrictions, it is not clear what is going on without the fourth event responder chain.

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.