2

I need to access this IBOutlet's value in another class.. How would I go about doing this?

Heres my SaveTextViewController.h class

#import <UIKit/UIKit.h>

@interface SaveTextViewController : UIViewController{
   IBOutlet UITextField *saveText;
}

@property (retain, nonatomic) IBOutlet UITextField *saveText;

@end

And here is my TextView.m class

#import "TextView.h"
#import "SaveTextViewController.h"

@implementation TextView

- (IBAction)saveTextView{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];

//Trying to access the returned variable "saveText" from IBOutlet in SaveTextViewController              
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"%d",saveText];

    NSString *savedString = self.text;
[savedString writeToFile:documentTXTPath atomically:YES];

NSLog(@"%@", documentTXTPath);

}

Thanks!

CodaFi
  • 43,043
  • 8
  • 107
  • 153
Walker Christie
  • 531
  • 1
  • 3
  • 18

3 Answers3

1

There is no clean way to just generically access the IBOutlet UITextField *saveText from any app. You can declare it in the .h file instead of the .m and have it accessible that way, assuming you can send a pointer to your SaveTextViewController class. But its a better practice to pass a pointer to your UITextFIeld to your TextView class from your SaveTextController class.

in TextView.h create a property

@interface TextView: UIView

@property (nonatomic, strong) UITextField *textField;

@end

in your SaveTextViewController.m create another property:

@interface SaveTextViewController : UIViewController{
   IBOutlet UITextField *saveText;
}

@property (retain, nonatomic) TextView *textView;
@property (retain, nonatomic) IBOutlet UITextField *saveText;

assign self.textView to your TextView

in your viewDidLoad method, set the textField in your TextView class like this:

self.textView.textField = self.saveText;

That would give you a clean connection between those two classes.

HalR
  • 11,411
  • 5
  • 48
  • 80
0

An IBOutlet is just behaving like any other property or instance variable. So if you want to pass that reference to another class, just do exactly that. Note that you do not need to specify an instance variable with the same name as the property. The current runtime will handle this itself.

If you want to pass a reference to the UITextField, add a property to your text view.

@interface TextView : UIView

@property (retain, nonatomic) UITextField *saveTextField;
...
@end

In your SaveTextViewController you can now set that property. You can do this in the viewWillAppear: method for example.

@implementation SaveTextViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.textView.saveTextField = self.saveText
}

@end

Also note that if you need a reference to the TextView and you created that in your nib/storyboard, add another IBOutlet to the SaveTextViewController.

#import "TextView.h"

@interface SaveTextViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *saveText;
@property (retain, nonatomic) IBOutlet TextView *textView;

@end

Finally I would also recommend you to rename saveText to saveTextField, because that's what it is. Also, you seem to be using unsanitized user input data from text fields and feed them into filenames. This is generally a bad idea™. I would recommend you to think about what you are actually trying to do. This is potentially dangerous and can lead to data loss and more fun stuff.

felinira
  • 1,644
  • 15
  • 21
-1

In

@implementation TextView

type

SaveTextViewController *saveTextViewController = [[SaveTextViewController alloc] init];
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:@"%d",saveTextViewController.saveText];

Hope this helps!

user2277872
  • 2,963
  • 1
  • 21
  • 22
  • Now it is throwing the error "too many arguments to method call".. How would I go about fixing this? http://i.imgur.com/1FbZG6j.png – Walker Christie Jul 31 '13 at 02:44
  • Uh... string literals aren't magically format-strings, they comprise format strings. – CodaFi Jul 31 '13 at 02:45
  • How would I go about converting the string? – Walker Christie Jul 31 '13 at 02:53
  • you can try adding '.text' onto the end of the saveTextViewController.saveText and then just change the @"%d" to @"%@" – user2277872 Jul 31 '13 at 02:55
  • Hmm that didn't work.. it is still complaining about too many arguments. Thanks for your help so far by the way. – Walker Christie Jul 31 '13 at 02:59
  • Oh, too many arguments....I wasn't even reading that right! type this in....NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",saveTextViewController.saveText.text]];....see if that helps you out – user2277872 Jul 31 '13 at 03:03
  • No problem :) glad I could be of some assistance – user2277872 Jul 31 '13 at 04:01
  • Grabbing the IBOulet from another object and asking for the text value is a [Law of Demeter](http://en.wikipedia.org/wiki/Law_of_Demeter) violation and tightly couples the two classes together. To fix this, SaveTextViewController should have a fileName method that returns an NSString, ie saveText.text. Then your other class won't have to have knowledge about the internals of SaveTextViewController, it could then just ask for the string, regardless of where it actually came from. – BergQuester Jul 31 '13 at 04:26