Question

So I have an app in Xcode that has a main view controller and an settings view controller. I want to have a segmented view in the settings view that triggers actions on the main view. The settings view controller is connected to its own class "settingsviewcontroller.h" and the view controller is connected to its own class "viewcontroller.h". So how do I do this?

Was it helpful?

Solution

One approach is to define a protocol:

SettingsDelegate.h:

@protocol SettingsDelegate
- (void)settingsUpdated:(NSDictionary *)newSettings
@end

Include this header in both of your view controllers. Have viewController conform to this protocol. Establish a weak reference from settings controller to viewcontroller, e.g.

SettingsViewController.m:

@interface SettingsViewController ()
@property (readwrite, weak) id<SettingsDelegate> settingsDelegate;
@end

When settingscontroller is instantiated, set self.settingsDelegate to the viewcontroller. Assuming you create the settingscontroller from the viewcontroller, it would be something like this:

viewController.m:

SettingsController *settingsController = [[SettingsController alloc] init];
settingsController.settingsDelegate = self;
// present the settingsController

In settings controller, when the settings are updated:

if (self.settingsDelegate) {
    NSDictionary *newSettings = ...; // set this value
    [self.settingsDelegate settingsUpdated:newSettings];
}

For more details, see Apple's Working with Protocols: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html

OTHER TIPS

*By the way I am not sure if this is right at all

The first way:

Try doing the -(IBAction)... connection. I know they are different views, but maybe you can still do the connection from different responders anyway.

The second way:

Try importing the .h/.m files so that you can access different variables etc... from the other class

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top