can't get form access from my class - objective-c

#import <Foundation/Foundation.h>
#interface MyClass : NSObject {
#private
IBOutlet NSTextField *tf;
}
- (void)setStr;
#end
===========================================
#import "MyClass.h"
#implementation MyClass
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)setStr
{
[tf setStringValue:#"aaaaaaaaa"];
}
#end
Call a method from my AppDelegate class
- (IBAction)test1:(id)sender
{
MyClass *m = [[MyClass alloc] init];
[m setStr];
}
I created an object of MyClass in .xib file. I correlated outlet of textfield with a textfield on form.
And there are no actions when button pressed.
where I'm wrong?

You need to create an outlet for MyClass in AppDelegate and connect it up, then use that outlet in test1 instead of creating a new instance. This guide from Apple should help you.
Another possibility is to put the test1 action into MyClass and have it called directly with the button press, or to make setStr an IBAction. These are possible, since you have a MyClass instance in your XIB.

Related

Modifying string content in NSTextView works under viewDidLoad method, but not under myMethod

I am trying to update the contents of an NSTextView that is connected to myViewController as a referencing outlet to the Files Owner which is the subclass myViewController.
When I use an IBAction from a button, or use the viewDidLoad method of the controller, I can update the text fine. However, when I try run the method from another class (referred to in this example as anotherViewController), it runs the method, but the textview does not change.
myViewController.h:
#import <Cocoa/Cocoa.h>
#import "anotherViewController.h"
#interface myViewController : NSViewController { }
#property (unsafe_unretained) IBOutlet NSTextView *outText;
#property (weak) IBOutlet NSButton *updateMeButton;
- (void)updateTextView:(NSString *)argText;
- (void)updateTextViewWithoutArg;
#end
myViewController.m:
#import "myViewController.h"
#interface myViewController ()
#end
#implementation myViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.outText.string = #"I work successfully";
}
- (IBAction)updateMeButton:(id)sender {
self.outText.string = #"I am updated text! I also work!";
}
- (void)updateTextView:(NSString *)argText {
self.outText.string = #"I don't make it to the NSTextView :(";
NSLog(#"Should have updated text view");
}
- (void)updateTextViewWithoutArg {
self.outText.string = #"I don't make it to the NSTextView :(";
NSLog(#"Should have updated text view");
}
#end
In anotherViewController.m , which has all the relevant imports, I call this:
myViewController *viewtask = [[myViewController alloc] init];
[viewtask updateTextViewWithoutArg];
Nothing happens. The method runs and logs that it should have updated, but no text updates. I have tried many different approaches, including textstorage and scrollrange methods, they all work the already working sections, but make no difference in the sections not working.
I've also tried just for fun:
myViewController *viewtask;
[viewtask updateTextViewWithoutArg];
Also using the instance variable _outText
Also using [self.outText setString:#"string"];
Also using [_outText setString:#"string"];
Again, they work but only in the already working sections.
This should be simple but isn't logical to me. In swift all I need to do is
self.outText.string = "I update whenever I'm called!"
Views you create in Interface Builder are lazily created, so if you access them before viewDidLoad is called they are nil.
If your case, calling
myViewController *viewtask = [[myViewController alloc] init];
does not cause the views to be created so when you call
[viewtask updateTextViewWithoutArg];
self.outText is nil.
You can see that this is what is happening by updating your code as below:
- (void)updateTextView:(NSString *)argText {
NSAssert(self.outText != nil, #"self.outText must not be nil");
self.outText.string = #"I don't make it to the NSTextView :(";
NSLog(#"Should have updated text view");
}
you should see the assert fire.
I appear to have found a solution by making myViewController a singleton class and using sharedInstance. For this particlar app, myViewController is a debug output window and will never need to be placed in another view.
I won't accept this answer yet, as it's not the best one I'm sure. There may still be a proper solution presented that allows finding the applicable myViewController instance, and modifying the outText property attached to it. Using this singleton makes subclassing tedious as I would have to make a new class for every instance if I wanted to be able to address say 10 View Controllers.
Anyway - the way I've been able to satisfy my simple requirement:
myViewController.h:
#import <Cocoa/Cocoa.h>
#import "anotherViewController.h"
#interface myViewController : NSViewController { }
#property (unsafe_unretained) IBOutlet NSTextView *outText;
#property (weak) IBOutlet NSButton *updateMeButton;
- (void)updateTextView:(NSString *)argText;
- (void)updateTextViewWithoutArg;
+ (id)sharedInstance;
#end
myViewController.m:
#import "myViewController.h"
#interface myViewController ()
#end
#implementation myViewController
static myViewController *sharedInstance = nil;
+ (myViewController *)sharedInstance {
static myViewController *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[myViewController alloc] init];
});
return sharedInstance;
}
- (void)viewDidLoad {
[super viewDidLoad];
sharedInstance = self;
}
- (void)viewDidUnload {
sharedInstance = nil;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.outText.string = #"I work successfully";
}
- (IBAction)updateMeButton:(id)sender {
sharedInstance.outText.string = #"Button Pressed";
}
- (void)updateTextView:(NSString *)argText {
sharedInstance.outText.string = argText;
}
- (void)updateTextViewWithoutArg {
sharedInstance.outText.string = #"I make it to the TextView now";
}
#end
Now when I use this code from within anotherViewController.m it updates the right instance:
[myViewController.sharedInstance updateTextView:#"Updating with this string"];

Delegate Method not Working

I'm trying to implement a delegate method for my button ... I think I did everything correctly but do not understand why I can not make it work ... My button does not respond to any commands looks dead ... ...
My button is located in the ViewController "B" and the function it has to perform is located in the ViewController "A"
This is my code
viewControllerB.h
#protocol UNI_TableLoginDelegate <NSObject>
-(void)showPassw;
#end
#interface viewControllerB : UITableViewController
#property (nonatomic, weak) id <UNI_TableLoginDelegate> delegate;
#end
viewControllerB.m (here I connected the action button via my storyboard)
#implementation viewControllerB
#synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)showReset:(id)sender {
[delegate showPassw];
}
viewControllerA.m
#import "viewControllerB.h"
#interface viewControllerA () <UNI_TableLoginDelegate>
- (void)viewDidLoad {
[super viewDidLoad];
viewControllerB *tableLogin = [[viewControllerB alloc] init];
tableLogin.delegate = self;
}
//Delegate Method
-(void)showPassw {
if (containerTable.frame.origin.y == 56) {
NSLog(#"ssss");
}
else {
NSLog(#"hdhdh");
}
}
You need to set the delegate in your ViewController A.
Let's say you have this method that calls right before dismissing the ViewController A:
-(void)dismissThisController{
// you need to set the delegate value
[self.delegate yourDelegateMethod:withValueYouWantToSend];
[self dismissViewControllerAnimated:YES completion:^{
}];
}
and in your View Controller B, you have this delegate method:
#pragma mark ViewController A delegate
-(void)yourDelegateMethod:(someType)value{
//receive your delegate value here
}

OS X Delegate set label from other window (Xcode)

I'm quite new to Mac programming (not to Objective C).
I'm developing a small application, that shows some data and opens a second window on button press.
In the second window is a textfield and a submit button. If the submit button is pressed, the window should close + the value of the textfield needs to be passed to the first window.
I think the best method for that is a simple Delegate. I tried that but i can't change the label in the first window using the second window..
The delegate however seems to work as i can call methods from the other class and send data to it. It just won't change the label.
As this is my first try on Delegates, im pretty sure I've done something stupid here^^
or is there a better solution? Can't be to complicated to change a label from an second window.. right?
ViewController.h (FirstController)
#import <Cocoa/Cocoa.h>
#class ViewController;
#protocol ViewControllerDelegate
-(void)sayHello:(ViewController *)ViewController;
#end
#interface ViewController : NSViewController
{
IBOutlet NSTextField *txtlabel;
}
#property (nonatomic, assign) id delegate;
-(void)helloDelegate;
-(void)reciveVar:(NSString*)strvar;
#end
ViewController.m (FirstController)
#import "ViewController.h"
#implementation ViewController
#synthesize delegate;
-(id)init {
self = [super init];
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
txtlabel.stringValue=#"TEST";
}
-(void)helloDelegate
{
[delegate sayHello:self];
}
-(void)reciveVar:(NSString*)strvar
{
NSLog(#"recived: %#", strvar);
txtlabel.stringValue=strvar; // DOSENT WORK!!
}
#end
secondController.h
#import <Cocoa/Cocoa.h>
#import "ViewController.h"
#interface secondController : NSViewController <ViewControllerDelegate>
{
IBOutlet NSTextField *txtfield;
}
-(IBAction)submit:(id)sender;
#end
secondController.m
#import "firstController.h"
#implementation secondController
-(void)viewDidLoad
{
[super viewDidLoad];
ViewController *custom = [[ViewController alloc] init];
// assign delegate
custom.delegate = self;
[custom helloDelegate];
}
-(void)sayHello:(ViewController *)ViewController
{
NSLog(#"Hiya!");
}
-(IBAction)submit:(id)sender
{
NSString *txtval= txtfield.stringValue;
NSLog(#"submit: %#", txtval);
ViewController *custom = [[ViewController alloc] init];
// assign delegate
custom.delegate = self;
[custom reciveVar:txtval];
}
#end
LOG Output:
Hiya!
submit: test
recived: test
(so i guess the delegate works..)
SOLVED. (Thanks to Phillip Mills)
NSNotification is way simpler and efficient than Delegates in this case.
ViewController.m
[...]
- (void)viewDidLoad
{
[super viewDidLoad];
txtlabel.stringValue=#"TEST";
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(handleUpdatedData:)
name:#"DataUpdated"
object:nil];
}
-(void)handleUpdatedData:(NSNotification *)notification
{
NSLog(#"recieved %#", notification);
txtlabel.stringValue=[notification object];
}
secondController.m
-(IBAction)submit:(id)sender
{
NSString *txtval= txtfield.stringValue;
NSLog(#"submit: %#", txtval);
[[NSNotificationCenter defaultCenter] postNotificationName:#"DataUpdated"
object:txtval];
}

NSPanel not showing.

My approach to this may be all wrong so I appreciate your patience.
I have a button in my main XIB file linked to this method in my document.m file:
- (IBAction)showTagModal:(id)sender {
if (!_FileTagWindowController){
_FileTagWindowController = [[FileTagWindowController alloc]init];
}
[_FileTagWindowController showWindow:self];
}
_FileTagWindowController is declared as a property in document.h and using breakpoints when the method is called, as far as I can tell is initializing properly, however _windowNibName and _window remains nil.
FileTagWindowController.h looks like this.
#import <Cocoa/Cocoa.h>
#interface FileTagWindowController : NSWindowController{
}
#property (strong) IBOutlet NSArrayController *tagsArray;
- (IBAction)saveContext:(id)sender;
#end
FileTagWindowController.m looks like this:
#import "FileTagWindowController.h"
#interface FileTagWindowController ()
#end
#implementation FileTagWindowController
- (id)initWithWindow:(NSWindow *)window
{
self = [super initWithWindow:window];
if (self) {
// Initialization code here.
}
return self;
}
- (void)windowDidLoad
{
[super windowDidLoad];
NSLog(#"Window Did Load!");
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
}
- (IBAction)saveContext:(id)sender {
}
#end
in my FileTagWindowController.xib I have File Owner set to FileTagWindowController as the custom class. I have the File Owner's "window" outlet linked to the window (NSPanel). That's all that should be required correct? The NSLOG statement in WindowDidLoad never gets called. I tried using [super initWithWindowNibName] in FileTagWindowController.m but that crashes not only the app, but Xcode as well with an endless initialization loop. Am I missing something obvious here?
Thanks all so much.
Try something like the following.
// document.h
#import "FileTagWindowController.h"
#property (strong) filetagWindowController *FileTagWindowController;
// document.m
#synthesize filetagWindowController;
- (IBAction)showTagModal:(id)sender {
if (self.filetagWindowController == nil) {
self.filetagWindowController = [[FileTagWindowController alloc] initWithWindowNibName:#"FileTagWindowController"];
}
[filetagWindowController showWindow:self];
[[filetagWindowController window] setReleasedWhenClosed:NO];
[NSApp runModalForWindow:filetagWindowController.window];
filetagWindowController = nil;
}
You may also want to call NSWindowWillCloseNotification to observe its state and see if filetagWindowController is closed.

UIView reference returns NULL pointed ARC enabled

I have ARC enabled so I am unsure as to why my reference is null.
My view controller instantiates a UIView ‘theGrid’ as soon as the view is loaded.
Later I have switch inside another class (MyOtherClass) that calls the UIViewContoller - (void) updateTheGrid:(id)sender method, that method is called as per the NSLog, but when I output the UIView to see if it is there, its returns null.
What am I doing wrong? It was my impression that ARC keeps up with everything. I feel like my trouble is coming from mm "MyOtherClass" when I ViewController * vc = [[ViewController alloc] init]; because I feel like that is just creating a new instance. But if that is the case, how am i suppose to reference the old instance and call the method?
NSLOG OUTPUT
[28853:c07] Intial Grid: <GridView: 0x8e423b0; frame = (0 0; 768 1024); layer = <CALayer: 0x8e43780>>
[28853:c07] Update The Grid (null)
GridView.h
#import <UIKit/UIKit.h>
#interface GridView : UIView
- (void) gridUpdated;
#end
GridView.m
#import "GridView.h"
#implementation GridView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSLog(#"initWithFrame");
}
return self;
}
- (void)drawRect:(CGRect)rect{
NSLog(#"Grid Draw Rect");
}
- (void) gridUpdated {
NSLog(#"GRID VIEW.m : Grid update called");
[self setNeedsDisplay];
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "GridView.h"
#interface ViewController : UIViewController {
GridView *theGrid;
}
#property (strong, retain) GridView * theGrid;
- (void) updateTheGrid : (id) sender;
#end
ViewController.m
#import "ViewController.h"
#import "GridView.h"
#interface ViewController () {}
#end
#implementation ViewController
#synthesize theGrid;
- (void)viewDidLoad {
[super viewDidLoad];
//draw the grid
theGrid = [[GridView alloc] initWithFrame:self.view.frame];
NSLog(#"Intial Grid: %#", theGrid);
[self.view addSubview:theGrid];
}
- (void) updateTheGrid : (id) sender{
NSLog(#"Update The Grid %#", theGrid);
[theGrid gridUpdated];
}
#end
MyOtherClass.m
- (void) mySwitch : (id) sender {
ViewController * vc = [[ViewController alloc] init];
[vc updateTheGrid:sender];
}
Do not allocate ViewController object again in your MyOtherClass.m because it will create an new instance of ViewController and your previous objects which holds ViewController wil get disposed including theGrid.
So please declare a weak property of ViewController inside the MyOtherClass.m and assign it while allocating MyOtherClass.m
Example:
ViewController class
moc = [[MyOtherClass alloc] initWithFrame:self.view.frame];
moc.vc = self;
MyOtherClass.h
#property(nonatomic,weak) ViewController *vc;
MyOtherClass.m
- (void) mySwitch : (id) sender {
[self.vc updateTheGrid:sender];
}
Note:Take care about the forward declarations :)