OS X Delegate set label from other window (Xcode) - objective-c

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];
}

Related

How to continouse pass value from one ViewController to second Viewcontroller and automatic close, dismiss 2nd ViewController

I try to pass data from one ViewController to secondControler however seem it not work. I use NSNotification.
- 2 Controller have same class "ViewController"
In viewcontroller.m
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(ProcessBarLoading) name:#"buttonPressed" object:nil];
}
-(void)ProcessBarLoading{
_labelTest.stringValue = #"TESTING";
}
- (IBAction)test:(id)sender {
[[NSNotificationCenter defaultCenter] postNotificationName:#"buttonPressed" object:self];
NSStoryboard *storyboard = [NSStoryboard storyboardWithName:#"Main" bundle: nil];
NSViewController * vc = [storyboard instantiateControllerWithIdentifier:#"SheetViewController"];
[self presentViewControllerAsSheet:vc];
}
When run program and press button, there're no update Label Text at all. Do you know why and how I can fix.
New Code:
In SecondViewController.m
#interface SencondViewController ()
#end
#implementation SencondViewController
#synthesize progressValue;
#synthesize labelView;
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
labelView.stringValue =progressValue;
}
In FirstViewCOntroller:
- (IBAction)test:(id)sender {
self->uide = #"0";
[self performSegueWithIdentifier:#"showRecipeDetail" sender:self->uide];
NSStoryboard *storyboard = [NSStoryboard storyboardWithName:#"Main" bundle: nil];
NSViewController * vc = [storyboard instantiateControllerWithIdentifier:#"SheetViewController"];
[self presentViewControllerAsSheet:vc];
- (void)prepareForSegue:(NSStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showRecipeDetail"]) {
SencondViewController * secondVC = segue.destinationController;
secondVC.progressValue = uide;
}
}
- (IBAction)test2:(id)sender {
uide = #"80";
[self performSegueWithIdentifier:#"showRecipeDetail" sender:uide];
[self.view displayIfNeeded];
}
So whether I press Button 1(test) other Button2 (test2) alway show new view with update value. What I need is only show 1 view.
Why do you need use a nsnotification the easy way is use a prepareForSegue or Delegation
This is an examample
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([segue.identifier isEqualToString:#"myId"]) {
SecondViewController *vc = segue.destinationViewController;
vc.myDataToPass = self.myValueInMyFirstViewController;
}
}
Notification pattern is not recommended for doing this. Use notification
when you want to pass some data to multiple objects on some event.
To solve this problem:
Step 1:
You should change your View Controller names to FirstViewController and SecondViewController, and have a property declared in your SecondViewController whose value you want to set from the FirstViewController.
Step 2:
Finally, in the prepare for Segue method of the FirstViewController, set the data.
In Objective-C, you can try this code:
#import "FirstViewController.h"
#import "SecondViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//This will trigger the prepareForSegue method
-(IBAction) someButtonClick {
[self performSegueWithIdentifier:#"YourSequeId" sender:nil];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
SecondViewController * secondVC = segue.destinationViewController;
secondVC.someValue = #"PassYourValueHere";
}
#end
and in the header file of the SecondViewController, declare the property:
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController
#property (nonatomic,strong) NSString *someValue;
#end
In the implementation file of the SecondViewController, write:
#import "SecondViewController.h"
#interface SecondViewController ()
#property (nonatomic,weak) IBOutlet UITextField *yourTextField;
#end
#implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.yourTextField.text = self.someValue
// Do any additional setup after loading the view.
}
#end

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
}

Changing the Value of NSTextField doesn't update label?

I'm trying to change a label from the AppDelegate. I can change the label with an IBAction that runs a changeLabel in the implementation of the class that has the label, but if I try to run changeLabel from the AppDelegate it changes the value (I have an NSLog), but doesn't update label.
Here's the code:
#import <Foundation/Foundation.h>
#interface testLabelThingy : NSObject
#property (strong) IBOutlet NSTextField *daLabel;
- (id) init;
- (void)changeLabel;
- (IBAction)daButton:(id)sender;
#end
and:
#import "testLabelThingy.h"
#implementation testLabelThingy
#synthesize daLabel;
- (id) init{
self.daLabel = [[NSTextField alloc] init];
return self;
}
- (IBAction)daButton:(id)sender{
[self changeLabel];
}
- (void)changeLabel{
NSLog(#"Change Label Function. Current value is: %#", [self.daLabel stringValue]);
if([[self.daLabel stringValue] isEqualToString:#"Bloog"]){
[self.daLabel setStringValue:#"Blarg"];
}else{
[self.daLabel setStringValue:#"Bloog"];
}
}
#end
For that you have to use NSNotificationCenter.
in Appdelegate use following code.
[[NSNotificationCenter defaultCenter] postNotificationName:#"ChangeThelabel" object:nil];
use the below code in the init method of the implementation of the class that has the label.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ChangelabelText:) name:#"ChangeThelabel" object:nil];
And in the same class use the following function.
- (void)ChangelabelText:(NSNotification *)notification
{
// Change the text here.
}

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 :)

Delegate - How to Use?

I think I understand the logic behind a delegate. I got more the problem to use it. How many steps are involved? Do I have to use existing delegates? Or can I use my one ones?
In my example I got the AppDelegate that created many views (Objects / View Controllers) of the same type. Each view should somehow call a method on the AppDelegate to close itself. This would happen when a button within the view is touched. The method call would include the reference of the view (self).
So far I know from other languages responders, event listeners and so on. They are so simple to use.
Can anybody help me. I just found massive examples with a lot of code in the web. It can't be that hard to just call a parent in Objective C.
I think you should use for this the NSNotificationCenter
in you AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
...
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(buttonPushed:) name:#"ButtonPushedNotification" object:nil];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
...
...
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
this is the selector it will be called when the notification happens (we are still in the AppDelegate.m)
- (void)buttonPushed:(NSNotification *)notification {
NSLog(#"the button pushed...");
}
and in the ViewController.m when the button pushed (inside the method), you should post a notification like this:
{
...
[[NSNotificationCenter defaultCenter] postNotificationName:#"ButtonPushedNotification" object:nil];
...
}
You can create your own:
In MyView1.h:
#class MyView1;
#protocol MyView1Delegate <NSObject>
- (void)closeMyView1:(MyView1 *)myView1;
#end
#interface MyView1 : NSObject
{
id<MyView1Delegate> _delegate;
}
#property (assign, nonatomic, readwrite) id<MyView1Delegate> delegate;
...
#end
In MyView1.m:
#interface MyView1
#synthesize delegate = _delegate;
...
// The method that tells the delegate to close me
- (void)closeMe
{
....
if ([_delegate respondsToSelector:#selector(closeMyView1:)])
{
[_delegate closeMyView1:self];
}
}
#end
In AppDelegate.h:
#import "MyView1.h"
#interface AppDelegate <MyView1Delegate>
{
MyView1 *_myView1;
}
...
#end
In AppDelegate.m:
- (void)someCreateViewMethod
{
_myView1 = [[MyView1 alloc] initWithFrame:NSMakeRect(0, 0, 100, 200)];
[_myView1 setDelegate:self];
...
}
An easy way to get what you want is to just start with one view. Then, have each other view be presented modally. When the button in the view is pressed do
[self dismissModalViewControllerAnimated:YES];
And here's something I made a while ago when I was starting iPhone development that might help you with delegates
Delegates
//In parent .m file:
//assign the delegate
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"segueName"])
{
childController *foo = segue.destinationViewController;
foo.delegate = self;
}
}
//implement protocol method(s):
- (void) methodName:(dataType*) dataName
{
//An example of what you could do if your data was an NSDate
buttonLabel.titleLabel.text = [[date description] substringToIndex:10];
}
//In parent .h file:
//import child header
#import "ChildName.h"
//indicate conformity with protocol
#interface ParentName : UIViewController <ChildNameDelegate>
//In child .h file
//declare protocol
#protocol ChildNameDelegate
- (void) methodName:(dataType*) dataName;
#end
//declare delegate
#property (unsafe_unretained, nonatomic) id<ChildNameDelegate> delegate;
//In child .m file
//synthesize delegate
#synthesize delegate;
//use method
- (IBAction)actionName:(id)sender
{
[delegate methodName:assignedData];
}