I'm trying my hand at some iPhone programming and I've run across something that may be fairly obvious to veterans but I'm not exactly sure why it's happening. I have two UIViewController classes, and I want to access a method from the other class. I have two NSObject classes associated with both of them in IB (with the Class file as UpdateClass for each), and I'm trying to create a class and call a method. Seems pretty easy, but the problem is that it's calling the method (according to NSLog) but it's not updating the labels. Here's my code:
//UpdateClass.h
#import <Foundation/Foundation.h>
@interface UpdateClass : NSObject {
IBOutlet UILabel *lbl1;
IBOutlet UILabel *lbl2;
}
@property (nonatomic, retain) IBOutlet UILabel *lbl1;
@property (nonatomic, retain) IBOutlet UILabel *lbl2;
- (void)updateLabels;
@end
//UpdateClass.m
#import "UpdateClass.h"
@implementation UpdateClass
@synthesize lbl1;
@synthesize lbl2;
- (void)updateLabels {
NSString *someWord = @"Whatever"; // This could be anything
NSLog(@"NSObject Update");
[lbl1 setText:someWord];
[lbl2 setText:someWord];
}
@end
//ViewController1.h
#import <UIKit/UIKit.h>
@class UpdateClass;
@interface ViewController1 : UIViewController {
IBOutlet UIButton *button1;
NSObject *UpdateClassObject;
UpdateClass *updateClass;
}
@property (nonatomic, retain) IBOutlet UIButton *button1;
@property (nonatomic, retain) NSObject *UpdateClassObject;
@property (nonatomic, retain) UpdateClass *updateClass;
- (void)updateLabels:(id)sender;
@end
//ViewController1.m
#import "ViewController1.h"
#import "UpdateClass.h"
@implementation ViewController1
@synthesize button1;
@synthesize UpdateClassObject;
@synthesize updateClass;
- (void)viewDidLoad {
[super viewDidLoad];
updateClass = [[UpdateClass alloc] init];
}
- (void)updateLabels:(id)sender; { //This is connected to TouchDown on button1
NSLog(@"Calls UpdateLabels");
[updateClass updateLabels]; //Calls the class method
}
//ViewController2.h
#import <UIKit/UIKit.h>
@class UpdateClass;
@interface ViewController2 : UIViewController {
IBOutlet UIButton *button2;
NSObject *UpdateClassObject;
UpdateClass *updateClass;
}
@property (nonatomic, retain) IBOutlet UIButton *button2;
- (void)updateLabels:(id)sender;
@end
//ViewController2.m
#import "ViewController2.h"
#import "UpdateClass.h"
@implementation ViewController2
@synthesize button2;
@synthesize UpdateClassObject;
@synthesize updateClass;
- (void)viewDidLoad {
[super viewDidLoad];
updateClass = [[UpdateClass alloc] init];
}
- (void)updateLabels:(id)sender; { //This is connected to TouchDown on button2
NSLog(@"Calls UpdateLabels");
[updateClass updateLabels]; //Calls the class method
}
So there is an NSObject hooked up in IB for both Views. There is a label on each view hooked up to the NSObject and the File's Owner (may not be necessary to hook them up to both). When the button is pressed (which is also hooked up properly in IB), the label is supposed to change to some string. NSLog reports that the methods are called, but the labels don't change. What's wrong here?
(note: there may be some small mistakes as I had to type out some of this because I don't have all the code with me at the moment).