【问题标题】:iOS Access parentViewController to change the State of a UIButton setSelected:YESiOS 访问 parentViewController 以更改 UIButton 的状态 setSelected:YES
【发布时间】:2013-02-10 20:25:22
【问题描述】:

我在 MainViewController 中有一个 UIButton。
MainViewController 有一个 childViewContoller。

我需要从 childViewController 访问 MainViewController 中的 UIButton (tcButton) 属性,并在 viewDidLoad 中将其设置为 setSelected:YES。我的 ChildViewController.m 文件中有以下代码,但它不工作。

#import "ChildViewController.h"
#import "MainViewController.h"
#import "CoreData.h"

@interface ChildViewContoller () 
@property (nonatomic, strong) CoreData *coreData;
@property (nonatomic, strong) MainViewController *mainViewController;
@end

@implementation ChildViewController
@synthesize coreData, mainViewController;

-(void)viewDidLoad 
{
    [super viewDidLoad];
    self.managedObjectContext = [(STAppDelegate *)[[UIApplication sharedApplication]  delegate] managedObjectContext];
    [[(mainViewController *)self.parentViewController tcButton] setSelected:YES];
}

【问题讨论】:

    标签: ios uibutton parentviewcontroller childviewcontroller


    【解决方案1】:

    你的代码有点乱。为什么要在 viewDidLoad 中创建自己的新实例?这是没有意义的。如果 ChildViewController 是真正的子视图控制器,那么您可以通过 self.parentViewController 访问父视图。 viewDidLoad 中只需要一行:

    -(void)viewDidLoad // Line 4
    {
    
        [[(MainViewController *)self.parentViewController tcButton] setSelected:YES]; // Line 8
    }
    

    【讨论】:

    • rdelmar:我也有同样的想法,第一次尝试:[self.parentViewController tcButton] setSelected = YES];这给了我错误:No Visible @interface 'UIViewController' 声明选择器'tcButton' 然后我开始搜索并看到上面的代码。但是,现在我正在尝试您的代码,但出现以下错误:Parse Issue Expected expression。光标箭头指向 (MainViewController *) 的右括号。是的,我最后确实有一个分号,我复制并粘贴了您的代码。谢谢。
    • @user1107173,哦,对不起,我打错了,应该是setSelected:YES(不是=)结尾。
    • 感谢您的快速回复。我将其更改为 setSelected:YES];而且我仍然得到相同的 Parse Issue Expected 表达式。光标箭头指向 (MainViewController *) 的右括号。
    • @user1107173,这绝对应该有效 - 编辑您的问题以显示您现在在该课程中的内容。你导入 MainViewController.h 了吗?
    • @user1107173,它应该是 MainViewController(大写 M)——你指的是类,而不是你的属性。如果您愿意,可以将您的属性分配给 (MainViewController *)self.parentViewController。
    【解决方案2】:

    您的代码中有几个问题,但执行您想要的主要想法是获取指向 mainViewController 的指针。有很多方法可以做到这一点,但这里有一个简单的例子,你可以如何实现这样的事情。例如,在 ChildViewContoller 的初始化程序中,您可以将指针传递给 mainViewController:

    @interface ChildViewContoller ()
    
    @property (nonatomic, strong) MainViewController *mainViewController;
    
    @end
    
    @implementation ChildViewContoller
    
    - (id)initWithMainViewController:(MainViewController *)mainViewController
    {
        self = [super init];
    
        if (self)
        {
            _mainViewController = mainViewController;
        }
    
        return self;
    }
    
    - (void)viewDidLoad
    {
        [_mainViewController.tcButton setSelected:YES];
    }
    
    @end
    

    请注意我没有测试上面的代码,但你可以明白。

    【讨论】:

    • 我收到 4 个代码错误:(1)self = [super init]; //不能在init系列中的方法之外分配给'self' (2)_mainViewController = mainViewController; //意外的接口名称'MainViewController'预期的表达式(3)return self; //void 方法 initWithMainViewController 不应返回值 (4)//在 ARC 中不允许将 Objective-C 指针隐式转换为 'void'。
    • 很抱歉,但正如我所说,我无法测试代码,请不要按原样使用它。而是尝试了解代码在做什么。我已经修改了一些代码以避免一些错误,请不要说这是一个 ARC 版本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    • 2018-09-06
    • 2020-05-25
    • 1970-01-01
    相关资源
    最近更新 更多