-1

In my project, I've declared a BOOL *isOn in my FirstViewController. In FirstViewController, I have two buttons where pressing buttonOne, sets isOn to YES, and buttonTwo sets isOn to NO. In my SecondViewController, I'm trying to run an if-statement that references the status of isOn. I'm getting error "Assigning to 'readonly' return result of an Objective-C not allowed". How may I get my desired intent?

//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 I just run it with...

if ([FVS isOn])  

The if-statement returns the else function and 'does that' for both buttons. Please help.

user2621075
  • 357
  • 4
  • 15
  • i wont think this would work. even with '==' operator. just nslog vc.isOn and i think it will display nil – Aravind G S Jan 26 '14 at 06:14
  • You're right, but can you offer any ideas to get it to work as I intend? – user2621075 Jan 26 '14 at 06:18
  • You should probably read [What's the best way to communicate between view controllers?](http://stackoverflow.com/questions/569940/whats-the-best-way-to-communicate-between-view-controllers) – Matthias Bauch Feb 28 '14 at 12:18

3 Answers3

4

The compiler error is because you forgot an equals sign, it should be

if ([FVC isOn] == YES)

with that said though, [FVS isOn] is equivalent to that, so your problem with it only running Do that doesn't lie there. There isn't enough code to figure out why that is happening: are you sure that FVC has been set to the correct value?

Ben Pious
  • 4,765
  • 2
  • 22
  • 34
  • Good observation but the result of my if-statement remains. You may be right about FVC though. How would I declare that in my SecondViewController viewDidLoad? Assigning FVC = FirstViewController in viewDidLoad does not do the trick. – user2621075 Jan 26 '14 at 06:08
  • Where do you get the reference to firstViewController? Are you making a new one inside of viewDidLoad by calling an init method? Assuming that your IBAction Methods are actually firing and that nothing else is setting isOn, the only other explanation I can see is that you don't actually have a pointer to the right instance of FirstViewController. – Ben Pious Jan 26 '14 at 06:39
  • i have added my answer below.. please check – Aravind G S Jan 26 '14 at 07:06
1

try: if(FVC.isOn == YES) Remember when you want to compare values you use == operator.

Oleksiy Martynov
  • 379
  • 2
  • 11
0

If you are using storyboards , you can pass the value from the first view controller to the secondViewController using

in ViewController:h

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

@interface ViewController ():UIViewController

@property (nonatomic) BOOL isOn;

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

@end

In 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];
}

and in SecondViewController.h

@interface SecondViewController : UIViewController

@property (nonatomic) BOOL isOn;

@end

in 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");
     }
  }
Aravind G S
  • 392
  • 3
  • 17
  • note that ibaction button is the action for both the buttons in the first view controller – Aravind G S Jan 26 '14 at 06:29
  • will mail you the complete code i tried if you give me your mail id – Aravind G S Jan 26 '14 at 06:38
  • yes please, jb12apps@gmail.com – user2621075 Jan 26 '14 at 07:49
  • I got your email. It's not working because in my project it goes; FirstVC ----> (tabbarcontroller) [SecondVC, ThirdVC]. So I'm getting error "-[TabBarViewController setIsOnSite:]: unrecognized selector sent to instance " Sorry I should have made that known. Do you have a suggestion to get around this? Thanks for the responses. – user2621075 Jan 26 '14 at 08:13
  • Your code has 'no issues' initially but then terminates app when I push button for seque. I'll play around with a bit. – user2621075 Jan 26 '14 at 08:17
  • did you 'embed in' navigation controller to the view controller after the tab bar controller – Aravind G S Jan 26 '14 at 08:55
  • No, there is no navigation controller. Just a single-view (FirstVC) controller sequed to a tabbarcontroller (SecondVC, ThirdVC). – user2621075 Jan 26 '14 at 16:58
  • I've successful passed information between SecondVC and ThirdVC within the tabbarcontroller, but I can't pass anything between FirstVC and SecondVC. Do you have any experience with this? – user2621075 Jan 26 '14 at 17:12
  • You can do it the same way as you did it in the SecondVC to ThirdVC – Aravind G S Jan 27 '14 at 13:14