gpt4 book ai didi

iphone - 如何在 TabbarController 中设置 viewControllers 的委托(delegate)?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:54 24 4
gpt4 key购买 nike

编辑:

我有带两个选项卡的 tabbarcontroller,第一个选项卡我有一个 viewController,第二个选项卡我有 navigationViewController 和 两个带有 tableView 的 ViewControllers 堆栈。

Tab1--->VC1

Tab2--->NVC-->fistVC1----push to----->secondVC2.

我要推送的代码:

拳头VC1.m

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0 & (indexPath.row == 0)) {

_secondVC2 = [[secondVC2 alloc] init];
[self.navigationController pushViewController:_secondVC2 animated:YES];
[_secondVC2 release];

what I need to do is set VC1 as a delegate of secondVC2, when secondVC2 cell selected i Want to send message to the VC1.

我该怎么做,请给我一些建议。

我试了一下:

第二个VC2.h

 @protocol secondVC2Delegate <NSObject>

- (void)someMethod;

@end
#import <UIKit/UIKit.h>

@interface secondVC2 :UIViewController<UITableViewDelegate,UITableViewDataSource>
{
id<secondVC2Delegate>delegate;
}

@property (nonatomic ,assign) id<secondVC2Delegate>delegate;

@end;

secondVC2.m

  - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"delegate%@",self.delegate);
if (indexPath.row == 0) {
[self.delegate someMethod];
}

VC1.h

    #import <UIKit/UIKit.h>
#import"secondVC2"
@interface VC1 :UIViewController<secondVC2Delegate>{

secondVC2 *tvc2;

}
@property (nonatomic ,retain) secondVC2 *tvc2;

- (void)someMethod;

@end;

VC1.m

   - (void)viewDidLoad
{
tvc2 = [secondVC2 alloc] init];
tvc2.delegate = self;
}

但是委托(delegate)一直被释放,我在 secondVC2 中得到了 nil 委托(delegate),我不知道为什么。 那么我怎样才能做到这一点?

最佳答案

ViewController1 *viewController1 = [[ViewController1 alloc] init];
ViewController2 *viewController2 = [[ViewController2 alloc] init];
viewController1.delegate = viewController2;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController2];
[self.tabBarController setViewControllers:@[viewController1, navigationController]];

您还可以查看 NSNotificationCenter

关于iphone - 如何在 TabbarController 中设置 viewControllers 的委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17689964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com