【问题标题】:Reference BOOL of Different View Controller [closed]不同视图控制器的参考 BOOL [关闭]
【发布时间】:2014-01-26 05:38:10
【问题描述】:

在我的项目中,我在我的 FirstViewController 中声明了一个 BOOL *isOn。在 FirstViewController 中,我有两个按钮,按下 buttonOne,将 isOn 设置为 YES,buttonTwo 将 isOn 设置为 NO。在我的 SecondViewController 中,我试图运行一个引用 isOn 状态的 if 语句。我收到错误“不允许分配给 Objective-C 的‘只读’返回结果”。我怎样才能得到我想要的意图?

//FirstViewController.h

@property (assign) BOOL isOn;

- (IBAction)buttonOne:(id)sender;

- (IBAction)buttonTwo:(id)sender;


//FirstViewController.m

- (IBAction)buttonOne:(id)sender {  
[self setIsOn:YES];
}

- (IBAction)offSiteButton:(id)sender {
[self setIsOn:NO];
}

//SecondViewController.m

#import "FirstViewController.h"
#import "FirstViewController.m"

FirstViewController *FVC

- (void)viewDidLoad {

if ([FVC isOn] = YES) {  <----Error

// Do this

} else {

// Do that

}

如果我只是用...运行它

if ([FVS isOn])  

if 语句返回 else 函数并为两个按钮“执行此操作”。请帮忙。

【问题讨论】:

标签: ios objective-c if-statement boolean viewcontroller


【解决方案1】:

编译器错误是因为你忘记了一个等号,应该是

if ([FVC isOn] == YES)

话虽如此,[FVS isOn] 与此等价,因此您仅运行Do that 的问题并不存在。没有足够的代码来找出发生这种情况的原因:您确定 FVC 已设置为正确的值吗?

【讨论】:

  • 很好的观察,但我的 if 语句的结果仍然存在。不过,您对 FVC 的看法可能是正确的。我将如何在我的 SecondViewController viewDidLoad 中声明它?在 viewDidLoad 中分配 FVC = FirstViewController 并不能解决问题。
  • 从哪里获得对 firstViewController 的引用?您是否通过调用 init 方法在 viewDidLoad 中创建一个新的?假设您的 IBAction 方法实际上正在触发,并且没有设置 isOn,那么我能看到的唯一其他解释是您实际上没有指向正确的 FirstViewController 实例的指针。
  • 我已经在下面添加了我的答案..请检查
【解决方案2】:

尝试: 如果(FVC.isOn == 是) 请记住,当您想比较值时使用 == 运算符。

【讨论】:

  • 确实如此,但 if 语句仍然返回 else。请参阅对本回答的评论。有什么想法吗?
【解决方案3】:

如果您使用的是故事板,则可以使用将值从第一个视图控制器传递给第二个视图控制器

在视图控制器中:h

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ():UIViewController

@property (nonatomic) BOOL isOn;

-(IBAction)button:(UIButton *) button;
-(IBAction)go;

@end

在 ViewController.m 中

@implementation ViewController

@synthesize isOn;

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    SecondViewController *vc = [segue destinationViewController];
    [vc setIsOn:isOn];
}

- (void)viewDidLoad
{
     [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

-(IBAction)button:(UIButton *)button
{
    switch (button.tag) {
        case 1:
        [self setIsOn:YES];
        break;

        case 2:
        [self setIsOn:NO];
        break;
      }
 }

-(IBAction)go
{
    [self performSegueWithIdentifier:@"next" sender:nil];
}

在 SecondViewController.h 中

@interface SecondViewController : UIViewController

@property (nonatomic) BOOL isOn;

@end

在 SecondViewController.m 中

@implementation SecondViewController

@synthesize isOn;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
      self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
      if (self) {
          // Custom initialization
      }
      return self;
  }

- (void)viewDidLoad
  {
      [super viewDidLoad];
     // Do any additional setup after loading the view.
     if (isOn == YES)
     {
          NSLog(@"Hello");
     }
     else
     {
         NSLog(@"exit");
     }
  }

【讨论】:

  • 请注意,ibaction 按钮是第一个视图控制器中两个按钮的操作
  • 如果你给我你的邮件ID,我会把我试过的完整代码邮寄给你
  • 是的,请 jb12apps@gmail.com
  • 我收到了你的电子邮件。它不起作用,因为在我的项目中它去了; FirstVC ----> (tabbarcontroller) [SecondVC, ThirdVC]。所以我收到错误“-[TabBarViewController setIsOnSite:]: unrecognized selector sent to instance” 对不起,我应该知道的。您有解决此问题的建议吗?感谢您的回复。
  • 您的代码最初“没有问题”,但当我按下按钮进行后续操作时会终止应用程序。我会玩一会儿。
猜你喜欢
  • 1970-01-01
  • 2017-01-12
  • 2017-01-06
  • 1970-01-01
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多