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.