0

I'm new in Objective-C and I'm making an app that has two tabs. One tab is where I read out the accelerometer and gyroscope and display this values with labels and progress bars, the second tab is were I can make some network setting like IP, Port, there's also a textfield for logs.

After some searching I found that the best option to pass data from my settings tab (NetworkViewController) and the first tab (MotionViewController) is to use delegates and protocols.

This is my NetworkViewController.h:

@protocol ViewControllerNetworkDelegate <NSObject>
- (void)PassDataToViewController:(NetworkViewController *)controller didFinishEnteringItem:(NSString *)item;
@end

@interface NetworkViewController : UIViewController

@property (nonatomic, weak) id<ViewControllerNetworkDelegate> delegate;

@property (strong, nonatomic) IBOutlet UITextField *addressField;
@property (strong, nonatomic) IBOutlet UITextField *portField;
@property (strong, nonatomic) IBOutlet UITextView *logField;

- (IBAction)toggleUDP:(id)sender;
- (IBAction)hideKeyboard:(id)sender;

@end

Fixed: I get an error on the method PassDataToViewController saying "Expected a type".

In my implementation file I have the following code in the method hideKeyboard so it gets exetuded when I change the textfield for the IP-address:

NSString *itemToPassBack =addressField.text;
[self.delegate PassDataToViewController:self didFinishEnteringItem:itemToPassBack];

MotionViewController.h:

#import "NetworkViewController.h"

@interface MotionViewController : UIViewController <ViewControllerNetworkDelegate> 

MotionViewController.m

- (void)viewDidLoad
{
NetworkViewController *networkViewController = [[NetworkViewController alloc] initWithNibName:@"MotionViewController" bundle:nil];
networkViewController.delegate = self;
[self pushViewController:networkViewController animated:YES];
}

- (void)PassDataToViewController:(id)controller didFinishEnteringItem:(NSString *)item{
NSString *host= item;
}

This is the relevant code I my project, it's based on this post Passing Data between View Controllers.

I get following error: No visible @interface for 'MotionViewController' declares the selector' pushViewController animated
On [self pushViewController:networkViewController animated:YES]; in MotionViewController.h

Many thanks in advance

Community
  • 1
  • 1

1 Answers1

0

You need to forward declare your NetworkViewController before using it in your protocol inside your header file, using

@class NetworkViewController;

This way the compiler knows about the class you are using before you are actually declaring it.

Johannes Lumpe
  • 1,762
  • 13
  • 16