Nasıl iki görünümü denetleyicileri arasında iletişim kurmak için basit bir temsilci ayarlayabilirim?
İki UİTableViewControllers var ve üst alt görünüm denetleyicisi değeri geçmek için bir temsilci kullanarak gerekir. Ne olduğunu biliyorum Ve sadece bir örnek takip etmek için basit bir görmek istedim.
Teşekkür Ederim
CEVAP
Basit bir örnek...
Hadi çocuk görünümü denetleyicisi UISlider
ve sürgünün değer bir temsilci ile üst geri geçmek istiyorum diyelim.
Alt görünümü denetleyicinin başlık dosyası içinde, temsilci türü ve yöntemlerini bildirin:
ChildViewController.h
#import <UIKit/UIKit.h>
// 1. Forward declaration of ChildViewControllerDelegate - this just declares
// that a ChildViewControllerDelegate type exists so that we can use it
// later.
@protocol ChildViewControllerDelegate;
// 2. Declaration of the view controller class, as usual
@interface ChildViewController : UIViewController
// Delegate properties should always be weak references
// See http://stackoverflow.com/a/4796131/263871 for the rationale
// (Tip: If you're not using ARC, use `assign` instead of `weak`)
@property (nonatomic, weak) id<ChildViewControllerDelegate> delegate;
// A simple IBAction method that I'll associate with a close button in
// the UI. We'll call the delegate's childViewController:didChooseValue:
// method inside this handler.
- (IBAction)handleCloseButton:(id)sender;
@end
// 3. Definition of the delegate's interface
@protocol ChildViewControllerDelegate <NSObject>
- (void)childViewController:(ChildViewController*)viewController
didChooseValue:(CGFloat)value;
@end
Alt görünümü denetleyicisi uygulaması, temsilci yöntemleri olarak gerekli Ara.
ChildViewController.m
#import "ChildViewController.h"
@implementation ChildViewController
- (void)handleCloseButton:(id)sender {
// Xcode will complain if we access a weak property more than
// once here, since it could in theory be nilled between accesses
// leading to unpredictable results. So we'll start by taking
// a local, strong reference to the delegate.
id<ChildViewControllerDelegate> strongDelegate = self.delegate;
// Our delegate method is optional, so we should
// check that the delegate implements it
if ([strongDelegate respondsToSelector:@selector(childViewController:didChooseValue:)]) {
[strongDelegate childViewController:self didChooseValue:self.slider.value];
}
}
@end
Üst görünüm denetleyicinin başlık dosyası ChildViewControllerDelegate
protokolünü uygulayan bildirin.
RootViewController.h
#import <UIKit/UIKit.h>
#import "ChildViewController.h"
@interface RootViewController : UITableViewController <ChildViewControllerDelegate>
@end
Üst görünüm denetleyicisi uygulaması, temsilci yöntemleri uygun şekilde uygulamak.
RootViewController.m
#import "RootViewController.h"
@implementation RootViewController
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
ChildViewController *detailViewController = [[ChildViewController alloc] init];
// Assign self as the delegate for the child view controller
detailViewController.delegate = self;
[self.navigationController pushViewController:detailViewController animated:YES];
}
// Implement the delegate methods for ChildViewControllerDelegate
- (void)childViewController:(ChildViewController *)viewController didChooseValue:(CGFloat)value {
// Do something with value...
// ...then dismiss the child view controller
[self.navigationController popViewControllerAnimated:YES];
}
@end
Bu yardımcı olur umarım!
Ne'In AngularJS?denetleyicileri a...
Ne'in iletişim kurmak için en iyi...
Nasıl iframe ve üst site arasında ilet...
Program aracılığıyla görünümler arasın...
Nasıl Android Hizmet Aktivite ile ilet...