Sahil Chaudhary
Sahil Chaudhary

Reputation: 503

issue while using switch

I have two view controllers and nibs. I populated one view controller with a toggle switch and declared this in its header file:

@public UISwitch *toggleSwitch;

and exposed it as a property like this:

@property (nonatomic,retain) IBOutlet UISwitch *toggleSwitch;

I also connected the switch with toggleSwitch outlet. Then I used this switch in my other view controller like this :

theViewControllerWhereIDeclaredTheSwitch.toggleSwitch.on = YES;

Though everything worked fine with the switch being ON by default but when I switched off the switch it threw an exception: "Thread 1: signal SIGABRT" in the main.m file. I get this error quite often while working with Xcode, this error is a real pain in my ass. Please help.

Upvotes: 0

Views: 87

Answers (2)

fBourgeois
fBourgeois

Reputation: 326

@werner is right.

When programming a Controller and a View associated, you are implementing the NVC Pattern. The idea is that you have this Controller object that is the brain between a View and a Model that holds datas. Hence what you should perform in your application is not share the switch state but update a BOOL value in your Model when triggering the switch and share the Model with the two Controllers in order to know the value in the two Controllers.

(CF: wikipedia)

Upvotes: 1

werner
werner

Reputation: 859

You should not share UI elements over multiple UIViewControllers.

A better approach would be to share a BOOL or even encapsulate the state in your own object inheriting from NSObject and pass that between the 2 UIViewControllers.

Upvotes: 5

Related Questions