Obj-C Sending Messages Between Classes

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.


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


#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 to the //original view, i know it is not efficient [self dismissModalViewControllerAnimated:YES]; } - (void)dealloc { [super dealloc]; } @end


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


#import "viewControllerB.h" #import "viewControllerA.h" @implementation viewControllerB - (IBAction)openA { //presents viewControllerA when a button is pressed viewControllerA *viewA = [[viewControllerA alloc] init]; [self presentModalViewController:viewA animated:YES]; } - (void)display:(NSInteger)myNum { NSLog(@"YES"); [label setText:[NSString stringWithFormat:@"%d", myNum]]; } @end

YES is logged successfully, but the label's text does not change. I have made sure that all of my connections in Interface Builder are correct, in fact there are other (IBAction) methods in my program that change the text of this very label, and all of those other methods work perfectly...

Any ideas, guys? You don't need to give me a full solution, any bits of information will help. Thanks.

-------------Problems Reply------------


viewControllerB *viewB = [[viewControllerB alloc] init];

you are instantiating a new viewControllerB. This is not the viewControllerB that (I presume) you have in your nib file. You should make an outlet for that and wire it up.

Otherwise, possibly instantiate it with [... initWithNibName:] from a nib, instead of just [... init], and then (either way) push the instantiated view controller using [self.navigationController pushViewController:viewB animated:YES], or by presenting it modally as you seem to have mastered already.

As a sidenote, maybe name the viewcontroller variable viewConB, since there is a clear and important distinction between views and view controllers. Furthermore, class names tend to start with upper case, and variables with lower case, to keep things clear.

Category:iphone Views:0 Time:2011-01-09

Related post

  • Sending Messages Between Classes Obj-C? 2010-12-16

    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 in

  • Simplest way to send messages between Matlab, VB6 and VB.NET programs 2009-05-16

    We are upgrading a suite of data acquisition and analysis routines from VB6 programs to a mixture of VB.NET, VB6, and Matlab programs. We want to keep the system modular (separate EXEs) so we can easily create specialized stand-alone analysis program

  • sending message to viewController from the application delegate 2009-12-07

    I'm new to obj C, I come from an AS3.0 background. Basically in this app I'm making, I'm using the applicationWillResignActive in the app delegate to react to a call coming in . I have a navigationController with a table view that just triggers anoth

  • Send messages between iPhone & Mac 2011-05-02

    I'm fairly novice in iPhone development, but trying to create what would seem a fairly simple application. I want to have 1 program running on my phone/simulator that has a slider and a button, and another receiving program on the mac, such as Proces

  • send message on email with attachments >> null object 2011-10-01

    Possible Duplicate: What is a NullReferenceException in .NET? SmtpClient client; MailMessage message; private void btnSend_Click(object sender, EventArgs e) { message = new MailMessage(username.Text,to.Text,subject.Text,message123.Text); client = new

  • Send messages to program through command line 2008-08-13

    I have this program, we'll call it Host. Host does all kinds of good stuff, but it needs to be able to accept input through the command line while it's running. This means it has to somehow send its other process data and then quit. For example, I ne

  • C# - Sending messages to Google Chrome from C# application 2008-09-29

    I've been searching around, and I haven't found how I would do this from C#. I was wanting to make it so I could tell Google Chrome to go Forward, Back, Open New Tab, Close Tab, Open New Window, and Close Window from my C# application. I did somethin

  • org.jgroups.protocols.UDP - failed sending message to null 2008-12-16

    [hannel,] 15:08:03,642 - ERROR - org.jgroups.protocols.UDP - failed sending message to null (61 bytes) java.lang.Exception: dest=/ (64 bytes) at org.jgroups.protocols.UDP._send(UDP.java:333) at org.jgroups.protocols.

  • How to send messages between Companies 2009-02-18

    I often see myself in the need of setting up automated transfer of messages/data/files between companies. Besides the monstrous EDIFACT, Odette and a few other "big" electronic data exchange standards every industry has a few dozen or even e few hund

  • Are there special considerations for a windows service to send messages to user windows? 2009-03-05

    I have to write a Windows Service application (no GUI) that will monitor an event, and if it occurs will send a standard windows message to an application. The handle of the application will be given to the service by a DLL which is then unloaded, so

  • send message to a window handle 2009-04-03

    I'm running an exe through which I get a handle of a control in another exe. Now what I want to do is send messages to the particular handle from my exe. --------------Solutions------------- You need to import the function using: [DllImport("user32.d

  • Sending Messages to Yahoo IM using PHP 2009-05-02

    I am trying to connect to an Yahoo Messenger account and send PMs using PHP or CURL. I downloaded the below class from phpclasses.org which connects to eBuddy Web Messenger(http://www.ebuddy.com/) to send messages to the desired IM The original code

  • Sending messages between users 2009-05-08

    I am building an application and it needs to have a feature whereby one user can send another user a message. The system can be the most basic type available in rails and that would suit me fine. Any ideas on how to go about this? Thanks. -----------

  • Sending Messages to Selected Ips 2009-06-25

    I have an array of ip address which contains a set of ip address, which is to be communicated from server. Example: There are 5 clients. 4's Ip address out of this 5 will be stored in an ip address array. The remaining one will be blocked at the time

  • Sending messages to MSMQ using a WCF service 2009-07-20

    I've created a WCF service to send a message to an MSMQ, I can get it to run and it looks like the message was sent but when looking at the queue it's not there. I've verified and the queue is not a transactional queue. Security is open so that every

  • Can I send messages to a JMS queue from outside the app server? 2009-08-25

    As I understand it, a J2EE container is required to include a JMS provider. Is it possible for a standalone Java application to send messages to a JMS queue provided by the container? If so, how do I access the JNDI lookups from outside the container

  • Send Message to Net (Similar to net send) 2009-08-28

    Im not sure whether this topic have been disscussed or not, but i have no idea what to do. In c#, i need to send message to other people in my networks. I made some researches, and all pointing to socket methods. With this method, logically it will w

  • Can you send messages using MSMQ to a machine not in the same domain? 2009-09-09

    I haven't read enough about or used MSMQ till now. For many years now, I've only known that they exist. So, forgive me if my question indicates lack of rudimentary knowledge on the subject. If I have to send a message from machine A to machine B, but

  • How to send message from iphone/touch to server(tomcat)? 2009-09-14

    I have native iphone/ipod touch application.After enter the infomation on that application it will store into server database,again send message to iphone/ipod.Using objective c how to do ? Please help me out... --------------Solutions------------- N

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.072 (s). 11 q(s)