Question

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!

Was it helpful?

Solution 3

In

@implementation TextView

type

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

Hope this helps!

OTHER TIPS

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top