passing data from parent to child - objective-c

In my app I'm trying to pass data from the parent view to the child view. However, when I run the app the delegate method isn't being called.
Here is the code on how I implemented the custom delegate:
parent.h
#protocol SLBWallViewControllerDelegate <NSObject>
- (void) pictureToBeUploaded:(id)picture;
#end
#interface SLBWallViewController : UIViewController <UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
- (IBAction)createPotButtonPressed:(id)sender;
#property (weak, nonatomic) id <SLBWallViewControllerDelegate> delegate;
#end
parent.m
[self.delegate pictureToBeUploaded:info[UIImagePickerControllerEditedImage]];//i'm taking the pic from a uiimagepicker
child.h
#interface SLBPostViewController : UIViewController <SLBWallViewControllerDelegate>
#property (weak, nonatomic) IBOutlet UIImageView *picture;
#end
child.m
#pragma mark - Wall View Controller Delegate
- (void)pictureToBeUploaded:(id)picture{
self.picture.image = picture;
}
Is there anything wrong or missing?

Well, the problem probably (edit: confirmed in the comments) is that self.delegate is nil at that point. So you send a message to nil and nothing happens of course. What you have to do is make sure that you assign a Child instance to delegate property before trying to send the message. Something like this for example:
//...
self.delegate = //.. a 'Child' instance
//...
[self.delegate pictureToBeUploaded:info[UIImagePickerControllerEditedImage]];

//(1) In Child in .h file create one property
#property (nonatomic,strong) UIImage * someImage;
//(2) In Child in .m file override the property
-(void)setSomeImage:(UIImage *)someImage{
_someImage=someImage;
self.picture.image = self.someImage;
}
//(3) In Parent in .m file set image to child's 'someImage' property
childsObject.someImage= UIImagePickerControllerEditedImage;

Related

Unit Testing in XCode (Obj C) With MVVM

I'm testing out some MVVM pattern stuff and seem to have gotten myself confused. Hoping someone here can clarify things for me.
So, what I did was set up a project and added a class that is a subclass of NSObject and called it RootViewModel.
Gave it one method:
- (void) rootTest {
NSLog(#"Booyeah!");
}
In ViewController I imported RootViewModel and made an IBOutlet for it.
#import "ViewController.h"
#import "RootViewModel.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UIButton *btnRunModel;
#property IBOutlet RootViewModel* myModel;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.myModel rootTest];
}
#end
Then in Storyboard I dragged an Object into the ViewController scene, named it RootModel and connected it to the myModel property in ViewController.
Run the app and it works as expected, Booyeah gets logged.
So now here's where I got messed up. I wanted to set up a unit test. So working in the default unit test file I imported ViewController and made it a property and instantiated it in the set up.
#import <XCTest/XCTest.h>
#import "ViewController.h"
#interface ObjectiveVMMVTests : XCTestCase
#property (nonatomic, strong) ViewController* myViewController;
#end
#implementation ObjectiveVMMVTests
- (void)setUp {
[super setUp];
self.myViewController = [[ViewController alloc] init];
}
Then I tried to create a test where I call the rootTest method.
- (void) testRootModel {
[self.myViewController.myModel rootTest];
}
But I get a compiler error saying myViewController has no property myModel. I assumed it would be there, not sure where I messed this up.
In your unit test, you are saying:
#import "ViewController.h"
That's great. So now the unit test knows that this is a class. But that is not where the myModel property is declared. It is declared in ViewController.m, making this a private property.
Move the property declaration into ViewController.h to make it public so the unit test can see it.
Like #matt said, the IBOutlet is not part of the public interface of ViewController. It's private, hidden in the implementation (.m) file.
You have at least two viable options:
Add #property IBOutlet RootViewModel* myModel; to the ViewController.h file to make it part of the public interface;
Add an interface definition to the ObjectiveVMMVTests unit test file that'll satisfy the compiler:
#interface ViewController ()
#property IBOutlet RootViewModel* myModel;
#end
The implementation of the -(RootViewModel*)myModel getter is there anyway, the compiler just needs to know that ViewController does respond to the message. (You could use performSelector if you weren't interested in the returned object.)

Objective-c - control outlet from other class

I just like to play with coding for a hobby, so probably a noob question;
I have a simple storyboard for MacOS with 2 views. Both have there own classes (main class and subclass). How can I control a outlet in the subclass from the main class?
for example
I have a button (IBAction) in the mainclass and a textfield (IBOutlet) in the subclass. I want to set the stringvalue for the textfield with a click on the button in main.
I have searched a lot last days but just don't get it. (or just need a push in the right direction)
EDIT after JingJingTao's answer:
I used the control-drag function to open the second window.
I tried the code JingJingTao gives, but the textfield doesn't respond to the action.
My classes look like this now:
ViewController.h
#import <Cocoa/Cocoa.h>
#interface ViewController : NSViewController
- (IBAction)newText:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "ViewController2.h"
#interface ViewController ()
#property (nonatomic) ViewController2 *subclass;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
}
- (void)newText:(id)sender {
self.subclass.textField.stringValue = #"button pressed";
}
#end
ViewController2.h
#import "ViewController.h"
#interface ViewController2 : ViewController
#property (nonatomic) IBOutlet NSTextField *textField;
#end
ViewController2.m
#import "ViewController2.h"
#interface ViewController2 ()
#end
#implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
}
#end
Update:
I've attached two screenshots of what it looks like in the storyboard for the first suggestion,
1) Add a view to your ViewController, set the class at the top right to 'YourView', 'YourView' is a just an NSView, add a textfield to it and hook it up.
2) Add YourView as a property to your ViewController, i.e. #property (nonatomic) IBOutlet NSView *yourView; and hook it up.
Let me know if there are any issues.
You just need to put the textfield in the public interface of your subclass, so you can access it in your main class, although it does sound like you're using inheritance and I don't think you need to but that's another topic :D.
Example:
In MainClassViewController.m
#interface MainClassViewController ()
#propert (nonatomic) Subclass *subclass;
#end
#implementation MainClassViewController
// I guess you already add your subclass to the main viewcontroller because they display on the same screen.
- (void)yourButtonTapMethod {
self.subclass.textfield.text = #"Your value";
}
In Subclass.h
#interface Subclass : NSObject
#property (nonatomic) IBOutlet UITextfield *textfield;
I use Cocoa Touch instead of Cocoa, so maybe it's NSTextfield for you. Please let me know if this does not answer your question, good luck.

UIPopoverControllerDelegate delegate methods not being called

I have a screen with a button. Clicking the button will display a popover with two buttons. I want to be able to use the following method when the popover is dismissed:
#pragma mark - UIPopoverControllerDelegate
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
NSLog(#"OrderViewController.m -popoverControllerDidDismissPopover start");
}
The popover view has its own class:
#import <UIKit/UIKit.h>
#import "MenuItemModel.h"
#interface SelectedItemsOptionsViewController : UIViewController
#property (weak) id <UIPopoverControllerDelegate> delegate;
#property (strong, nonatomic) MenuItemModel *item;
#end
The class which creates the popover has:
#interface OrderViewController : UIViewController <PageViewControllerDelegate,
UITableViewDataSource, UITableViewDelegate, ServiceConnectorDelegate,
UIPopoverControllerDelegate>
So the OrderViewController - wants to know when SelectedItemsOptionsViewController has been dismissed.
I have the method declared (first block of code) and I am setting the delegate as:
SelectedItemsOptionsViewController *destViewController = (SelectedItemsOptionsViewController *)segue.destinationViewController;
popSegue = (UIStoryboardPopoverSegue *)segue;
[destViewController setDelegate:self];
destViewController.item = toDisplay;
So I am setting the delegate OK as far as i'm aware... the delegate property is #syntheized in th SelectedItemsOptionsViewController.m and well, its driving me a bit crazy.
I could be misunderstanding something here but looks like you're assigning the delegate for a UIViewController class, but where are you assigning the popOver its delegate, in the storyboard?

Objective-C: Delegate is nil in modal in navigation controller

I've got a ViewController inside a navigation controller that needs to present a modal.
The ViewController has this as its header:
#interface ViewController : BaseViewController<AuthenticateDelegate>
and in the IBAction that presents the modal:
AuthenticationController *authVC = [self.storyboard instantiateViewControllerWithIdentifier:#"AuthControllerView"];
authVC.delegate = self;
[self presentModalViewController:authVC animated:YES];
The AuthenticationController has this in its .h file:
#interface AuthenticationController: BaseViewController<UITextFieldDelegate>
#property (nonatomic, assign) id <AuthenticateDelegate> delegate;
#end
As you can see, I have assigned "self" (the ViewController) as the delegate to the AuthenticationController, but for some reason, the delegate is in:
- (IBAction)SubmitAuthentication:(id)sender;
{
[self.delegate validateUser:lblUsername.text :lblPassword.text];
[self dismissModalViewControllerAnimated:YES];
}
Any help will be appreciated.
you must create delegate property as below.
#property (nonatomic, strong) id <AuthenticateDelegate> delegate;

How to proxy editingDidEnd from UITableViewCell to UITableViewController?

I have a custom UITableViewCell:
#interface EditingTableViewCell : UITableViewCell <UITextFieldDelegate> {
UITextField *editableText;
UILabel *textLabel;
}
And I did receive editingDidEnd message from UITextField editableText. Now how can I pass it's message to UITableViewController which creates this cell?
Is creating ivar like this id delegate; within cell and set it to the tableViewController instance an only way?
I think i found a way to do a good job...
Create #protocol for this cell
// EditingCellDelegate.h
#protocol EditingCellDelegate
#optional
- (void)textLabelEditDidEnd:(UITextField *)textField;
#end
Add ivar into EditingTableViewCell class
// EditingTableViewCell.h
...
#property (nonatomic, assign) id <EditingCellDelegate> delegate;
So this way i can pass message to the UITableViewController that conforms that cell's protocol. Of course you have to check that class conformsToProtocol and respondsToSelector. Now I can call it. Something like this:
//EditingTableViewCell.m
- (IBAction)editDone:(id)sender {
if([[self delegate] respondsToSelector:#selector(textLabelEditDidEnd:)]) {
[[self delegate] textLabelEditDidEnd:(UITextField *)sender];
}
}