0

Hey I just had a question regarding XCode's behavior with multiple views implementing the same UIView class of my own creation. I am working with a tabbed application and controller, and I have multiple views on the storyboard, all of which implement a class that I created. On one of the views, I have a text field and a button, and on another, I have a text view with a startup text reading "Waiting...". As you can probably guess, I want to enter text into the text field on the first view, press the button, then display the proper output text in the textview on the other view.
My question is: is there a problem with implementing the same class between multiple views?
I have researched numerous discussions on the TextView method of setting text inside of it, but all of the suggestions between the forums say something different, and none of the methods seem to work appropriately.

[textView setText string] doesn't want to work when I switch to the other tab, textView.text = @"Message here" doesn't work either

I'd appreciate your help, and I've attached my code for reference.

#import "MasterController.h"

@interface MasterController ()

@end

@implementation MasterController
@synthesize input;
@synthesize output;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [self setInput:nil];
    [self setOutput:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)generate:(id)sender 
{
    [output setText:input.text];
}
- (IBAction)textFieldReturn:(id)sender
{
    [sender resignFirstResponder];
}
- (void)dealloc 
{
    [input release];
    [output release];
    [super dealloc];
}
@end

//MasterController.h
#import <UIKit/UIKit.h>

@interface MasterController : UIViewController
- (IBAction)generate:(id)sender;
- (IBAction)textFieldReturn:(id)sender;
@property (retain, nonatomic) IBOutlet UITextField *input;
@property (retain, nonatomic) IBOutlet UITextView *output;

@end
Shegit Brahm
  • 725
  • 2
  • 9
  • 22
Shelby Vanhooser
  • 58
  • 1
  • 1
  • 5

1 Answers1

1

If you have several views that are controlled by the same view controller, they will not communicate with each other in the way that you are trying to make them. When you call [output setText:input.text] , you are saying: set the text for the output text field for the view that you are currently on.

One somewhat hacky way of getting around this is to create a second view controller and have it inherit from your "Master." Variables are set as protected as default and will retain their information when subclassed.

If you want to communicate between the different view controllers properly, however, you should look into state injection in this question: What's the best way to communicate between view controllers? Or use a communication system such as NSNotification center. Or you could use NSCoding, all of which are fairly easy to implement.

Community
  • 1
  • 1
Ahab
  • 311
  • 1
  • 2
  • 6
  • It seems that the method of inheriting from the Master might by nice so I can pass data around easily between the two. How exactly would I go about doing this though? Simply make another class and say that it doesn't implement UIViewController, but rather MasterController? – Shelby Vanhooser Jul 05 '12 at 16:45
  • If you do inherit, you won't be able to pass data back up the chain, only down. But if its really what you want to do, yes just implement MasterController and set the View in your storyboard as the SubviewController. If setting the text for the textfield doesn't work, creating an NSString instance variable and setting the text there will. This really is not best practice though. – Ahab Jul 05 '12 at 16:52
  • After looking at the NSNotification center approach in the article you suggested, that looks like a really nice way of passing around the output and messages that I need between the tabs. I'll go with making a unique class for each view so it can do its thing, then I'll make it notify the center, which the output tab will be listening for. Many thanks Ahab. – Shelby Vanhooser Jul 05 '12 at 16:56