开发者

Sending Messages Between Classes Obj-C?

开发者 https://www.devze.com 2023-01-31 01:45 出处:网络
I\'m a newbie in iPhone Programming. I\'m trying to send a message from one view controller to another. The idea is that viewControllerA takes information from the user and sends it to viewControllerB

I'm a newbie in iPhone Programming. I'm trying to send a message from one view controller to another. The idea is that viewControllerA takes information from the user and sends it to viewControllerB. viewControllerB is then supposed to display the information in a label.

viewControllerA.h

#import <UIKit/UIKit.h>
@interface viewControllerA : UIViewController
{
int num;
}
-(IBAction)do;
@end

viewControllerA.m

#import "viewControllerA.h"
#import "viewControllerB.h"

@implementation viewControllerA

- (IBAction)do {
//initializing int for example
num = 2;
viewControllerB *viewB = [[viewControllerB alloc] init];
[viewB display:num];
[viewB release];
//viewA is presented as a ModalViewController, so it dismisses itself to return开发者_JAVA百科 to the 
//original view, i know it is not efficient but it is not the problem with my code 
[self dismissModalViewControllerAnimated:YES];
}

- (void)dealloc {
[super dealloc];
}

@end

viewControllerB.h

#import <UIKit/UIKit.h>
@interface viewControllerB : UIViewController
{
IBOutlet UILabel *label;
}
- (void)display:(int)myNum;

@end

viewControllerB.m

#import "viewControllerB.h"
#import "viewControllerA.h"

@implementation viewControllerB

- (void)display:(int)myNum {
NSLog(@"YES");
[label setText:[NSString stringWithFormat:@"%d", myNum]];
}

@end

YES is logged successfully, but the label's text does not change. Can sent messages not access instance variables or something?

Thanks.


There is nothing wrong with sending messages the way you are trying. The error is in using 'int' as a type. Use NSInteger in Objective-C to alleviate numerous headaches.


It seems as though your label is not pointing to anything (so when you tell it to setText:, that message is just being sent to nil and thus nothing happens).

Check Interface Builder to make sure you've set the outlet to actually point to the label in your view.


Is there more to your code than what you show here? I don't see how viewB is being shown -- there isn't a [self.view addSubview:viewB.view] or anything to indicate that it would be shown in a view. Perhaps that's the problem?


Replace all int with NSInteger.it will solve your problem.

Edit:

Ok,if you have proper connection.

actually what happens, you call the show function from viewA and try to write the text over the label of viewB from here before loading that page dats why you face problem.so for solving this problem you need to send value to viewB and write the text over label in viewDidLoad method. that will solve your problem.


Can you clarify what you're doing here, and how these view controllers relate to what's onscreen? My understanding is that viewControllerA is presented as some kind of modal dialog that gathers information? It's presented by viewControllerB, and when A closes B sets its label onscreen to reflect the value returned from A?

If this is the case, you need to have a reference to B in A, for a couple reasons. -First, you're initting an instance of B in A's do function and telling it to display your number, but I don't see you putting B onscreen anywhere. -Second, calling dismissModalViewControllerAnimated: on A won't dismiss A, it will dismiss the modal view controller that A presented. So to dismiss A, you need to say [self dismissModalViewControllerAnimated:YES]; on B. That's best accomplished by having A notify B that it's ready to be closed.

0

精彩评论

暂无评论...
验证码 换一张
取 消