Ios 如果我在基本UIViewController中定义了一个协议,它的子级是否能够符合objective-c中的协议?
我有:Ios 如果我在基本UIViewController中定义了一个协议,它的子级是否能够符合objective-c中的协议?,ios,objective-c,cocoa-touch,uiviewcontroller,uitableview,Ios,Objective C,Cocoa Touch,Uiviewcontroller,Uitableview,我有: UITableViewControllerA(列出了优化数据库产品结果的不同方法) UIViewControllerB(与上面列表中的一行关联的控制器) BaseController(UIViewController的子类UIViewController,包含与UITableViewControllerA中的行关联的控制器的通用代码,是它们的超类) 我可以使用perpareForSegue方法轻松地将数据从UITableViewControllerA传递到UIViewController
UITableViewControllerA
(列出了优化数据库产品结果的不同方法)
UIViewControllerB
(与上面列表中的一行关联的控制器)
BaseController(UIViewController的子类UIViewController
,包含与UITableViewControllerA
中的行关联的控制器的通用代码,是它们的超类)
我可以使用perpareForSegue方法轻松地将数据从UITableViewControllerA
传递到UIViewControllerB
。我现在尝试将数据从UIViewControllerB
传递到UITableViewControllerA
当在我的UIButton
自定义方法中调用popViewControllerAnimated时,用户一旦选择了要优化数据库产品结果的内容,就会点击该方法
我决定使用授权UIViewControllerB
是BaseController的子类,在我上面提到的UITableViewControllerA中,我有一个改进产品的方法列表:
性别-尺码-颜色-价格-类型
我没有在下面的每个控制器中定义我的协议,而是决定在BaseController中这样定义:
#import <UIKit/UIKit.h>
@protocol BaseViewControllerDelegate <NSObject>
- (void)didTapDoneButtonWithSelection:(NSString *)selection; // use string for now to test
@end
@interface BaseViewController : UIViewController
@property (nonatomic, retain) id <BaseViewControllerDelegate> delegate;
- (UIButton *)clearButton;
- (UIButton *)doneButton;
@end
- (void)doneButtonTapped:(id)sender {
NSLog(@"DONE BUTTON TAPPED");
[[self delegate] didTapDoneButtonWithSelection:@"THIS IS A TEST"];
[[self navigationController] popViewControllerAnimated:YES];
}
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "BaseController.h"
@interface UITableViewControllerA : UITableViewController <BaseViewControllerDelegate>
@end
@interface UITableViewControllerA ()
@end
@implementation UITableViewControllerA
{
NSString *_test;
}
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"%@", _test);
}
- (void)didTapDoneButtonWithSelection:(NSString *)selection
{
_test = selection;
}
在UITableViewControllerA中,我遵守委托协议:
#import <UIKit/UIKit.h>
@protocol BaseViewControllerDelegate <NSObject>
- (void)didTapDoneButtonWithSelection:(NSString *)selection; // use string for now to test
@end
@interface BaseViewController : UIViewController
@property (nonatomic, retain) id <BaseViewControllerDelegate> delegate;
- (UIButton *)clearButton;
- (UIButton *)doneButton;
@end
- (void)doneButtonTapped:(id)sender {
NSLog(@"DONE BUTTON TAPPED");
[[self delegate] didTapDoneButtonWithSelection:@"THIS IS A TEST"];
[[self navigationController] popViewControllerAnimated:YES];
}
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "BaseController.h"
@interface UITableViewControllerA : UITableViewController <BaseViewControllerDelegate>
@end
@interface UITableViewControllerA ()
@end
@implementation UITableViewControllerA
{
NSString *_test;
}
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"%@", _test);
}
- (void)didTapDoneButtonWithSelection:(NSString *)selection
{
_test = selection;
}
问题:
#import <UIKit/UIKit.h>
@protocol BaseViewControllerDelegate <NSObject>
- (void)didTapDoneButtonWithSelection:(NSString *)selection; // use string for now to test
@end
@interface BaseViewController : UIViewController
@property (nonatomic, retain) id <BaseViewControllerDelegate> delegate;
- (UIButton *)clearButton;
- (UIButton *)doneButton;
@end
- (void)doneButtonTapped:(id)sender {
NSLog(@"DONE BUTTON TAPPED");
[[self delegate] didTapDoneButtonWithSelection:@"THIS IS A TEST"];
[[self navigationController] popViewControllerAnimated:YES];
}
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "BaseController.h"
@interface UITableViewControllerA : UITableViewController <BaseViewControllerDelegate>
@end
@interface UITableViewControllerA ()
@end
@implementation UITableViewControllerA
{
NSString *_test;
}
- (void)viewWillAppear:(BOOL)animated
{
NSLog(@"%@", _test);
}
- (void)didTapDoneButtonWithSelection:(NSString *)selection
{
_test = selection;
}
我的日志消息给了我(空)
我所做的可能吗?还是我走错了方向。如果是这样,请用代码示例纠正我
谢谢你的时间
关于编辑:我想最初误解了这个问题 如果您使用的是segues,则正确答案是另一个。如果你不是,那么它是: 在UITableViewControllerA中,当您要按下BaseViewController时,它应该如下所示:
BaseViewController* base = [[BaseViewController alloc] init];
base.delegate = self;
[self.navigationController pushViewController:base animated:YES];
我必须获取UIViewControllerB的一个实例,并在prepareForSegue方法中将UITableViewControllerA设置为它的委托:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UITableViewControllerA *tvca = [segue destinationViewController];
[tvca setDelegate:self];
}
然而,这感觉很奇怪,好像我应该用另一种方法来做这件事。登录
didTapDoneButtonWithSelection
。根本没有日志。你设置了代理实例吗?@MatthewClark我所做的就是上面显示的。我想这就是我错过的。让我试试。这对我不管用。没想到将数据传回前一个控制器会如此困难。