Delegation issue in SplitView Controller - ios

I am implementing one simple delegation between the Master and Detail views (Landscape orientation so both views are visible) in a SplitViewController. When I press a button, the table view in Detail should update.
Here is the declaration in
MasterViewController.h
#protocol DetailVCDelegate <NSObject>
- (void)requestTableUpdate;
#end
#interface MasterViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
- (IBAction)updateTable:(id)sender;
#property (nonatomic, weak) id <DetailVCDelegate> delegate;
in MVC.m i Do the following for the IBaction updateTable:
[self.delegate requestTableUpdate];
Then in the Detail.h
#import "MasterViewController.h"
#interface DetailViewController : UITableViewController <DetailVCDelegate>
And in the Detail.m
- (void)requestTableUpdate
{
NSLog(#"called");
[self.tableView reloadData];
}
The log is never called, nor the update for the tableView.
I would appreciate any suggestions of how to fix this, i guess I am missing some delegation rule that is specific for SplitViews

When you send a message to something and nothing happens, your first thought should be: hmm, maybe I'm sending a message to nil.
So, you say:
[self.delegate requestTableUpdate];
But nothing happens. So you should immediately check to see whether self.delegate is nil.
You have a declared a delegate property:
#property (nonatomic, weak) id <DetailVCDelegate> delegate;
But a property has no value until you give it one. It is up to you to set the delegate property of your MasterViewController instance to some other instance (of something that adopts the DetailVCDelegate protocol, obviously). Delegates are not born: they are made, deliberately.

Related

Setting the delegate from third to first viewController

I have three viewControllers. When I'm on the third view controller I want to send a message to the first one. I'm using a protocol and trying to set the delegate for this.
viewControllerC.h
#protocol ViewControllerCDelegate
- (void)performAction;
#end
...
#property (nonatomic, strong) id<ViewControllerCDelegate> delegate;
viewControllerA.h
#interface ViewControllerA : UIViewController <ViewControllerCDelegate>
viewControllerA.m
...
- (void)performAction {
NSLog(#"Action was performed");
}
So the only problem is, I can't set the delegate from the third to the first. How can I set viewControllerC's delegate to viewControlerA?
Here's an image to describe it:
You'd have to pass along the A controller through to the B controller to be set as the delegate for C when it is created. Kind of messy.
In this case though it might make more sense to use a notification model where Controller A listens for a NSNotification that the action was completed on C.

Create IBAction of UIButton in one view controller and influence the action in another view controller

I am planning to create two UIViewController. Basically, there are some buttons in one of the two view controllers. When I press one of these buttons, I want to trigger some action in another view controller. Are there some ways to make it possible?
There are so three possible ways to do that,
Using NSNotificationCenter, Using Delegates and the last one is using Blocks, The first option is easy to learn.
Add the observer in to FirstViewController
Post notification in SecondViewController (when user clicks the button)
For reference follow this link
Hope this helps
Yes, you can use a delegate for that.
For example, you may have this ViewControllers.
//FirstViewController.h
#protocol ProtocolName <NSObject>
- (void)doSomething;
#end
#interface FirstViewController
#property (nonatomic, strong) id<ProtocolName> delegate;
#property (weak, nonatomic) IBOutlet UIButton *button;
- (IBAction)action:(id)sender;
#end
Here we need a protocol, who defines one or more methods to be implemented in any Object who implement this protocol. And also we need a delegate, that is an object who implements this protocol (in this case called "ProtocolName", but you can name it as you wish).
And then a second ViewController
//SecondViewController.h
#import "FirstViewController.h" //Need this to reference protocol
#interface SecondViewController <ProtocolName>
#end
With we are saying that SecondViewController will implement ProtocolName protocol, so in his .m file we need to do this.
//SecondViewController.m
- (void)doSomething{
//Do something
}
And here comes the magic.
Let's say that when you tap the button in FirstViewController it triggers the doSomething method in SecondViewController. So, you need to do something like this.
//FirstViewController.m
//...
SecondViewController secondVC = [[SecondViewController alloc] init];
this.delegate = secondVC; //DON'T FORGET THIS
//...
- (IBAction)action:(id)sender{
[this.delegate doSomething];
}
And this is the delegate pattern.

iOS MVC implementation with custom views

When the views are simple, their IBActions and IBoutlets are in viewcontroller, viewcontrollers assigns respective models to be loaded and viewcontroller get notified when models are prepared.
As My project contains lot of custom views for each viewcontroller, I want to implement actions in custom view itself and set data from controller (ViewController).
I should be able to use the same controllers and models for both iPhone and iPad where only UI changes.
I am concerned about how to pass data from view to viewcontroller and displaying data back on view when model changes?
Can anyone please suggest me to pass data between views <---> viewcontroller (controller) <---> model?
To do this I use Delegate design-pattern. It looks like this :
MyView.h
#protocol MyViewDelegate <NSObject>
- (void)customViewDidSomething;
#end
#interface MyView : UIView
#property (nonatomic, assign) id<MyViewDelegate> delegate
#end
MyView.m
- (void)userDidSomething {
[_delegate customViewDidSomething];
}
MyViewController.h
#import "MyView.h"
// ViewController has to implement the protocol
#interface MyViewController <MyViewDelegate>
#property (nonatomic, retain) IBOutlet MyView myView;
MyViewController.m
- (void)viewDidLoad { // Set the delegate somewhere
_myView.delegate = self
}
- (void)customViewDidSomething {
// Ok VC is aware that something happened
// Do something (tell subview to do something ?)
}
Instead of using different custom views, try using a UIViewController and then use the viewcontroller's view to display your UI. Also, this will also ensure that you will be able to communicate between the views and controller efficiently without confusion.

Set up Protocol with Modal View Controller -> ViewController

I have an initial ViewController, lets call it HomeViewController, that has a button which calls a modal view controller, lets call it ModalViewController. Inside that ModalViewController I have a table view with two sections. If you click on any cell from section 0 it sends information back to HomeViewController (this part I have working with Protocol). If you click on any of the cells from section 1 it pushes to another view controller with options, lets call it OptionsViewController. Here is where it gets tricky for me. If you click any of those options, dismiss OptionsViewController and close ModalViewcontroller and send that information to HomeViewController, just like ModalViewController to HomeViewController. I have tried to set up a protocol similar to the one in ModalViewController but it is never called.
OptionsViewController protocol & .h file
#protocol OptionsViewControllerDelegate <NSObject>
#optional
-(void) optionsInfo:(NSArray *)optionsViewArray;
#end
#interface OptionsViewController : UITableViewController
#property (retain) id delegate;
#property (nonatomic, strong) NSArray *sendArray;
#end
OptionsViewController.m where it's called to pop off the stack.
{
[self dismissOptionsView];
}
-(void) viewWillDisappear:(BOOL)animated
{
NSLog(#"Send Array: %#", self.sendArray);
[[self delegate] optionsInfo:sendArray];
}
-(void)dismissOptionsView
{
[self.navigationController popViewControllerAnimated:YES];
}
Inside ModalViewController.h
#protocol ModalViewControllerDelegate <NSObject>
#optional
-(void) sendInformation:(NSArray *)infoArray;
#end
#interface ModalViewController : UITableViewController <ConditionsViewControllerDelegate, UISearchBarDelegate>
{
UISearchBar *searchDrugBar;
}
#property (retain) id delegate;
#property (nonatomic, strong) IBOutlet UISearchBar *searchDrugBar;
#property (nonatomic, strong) NSArray *infoArray;
#end
ModalViewController.m where OptionsInfo is supposed to come in.
-(void) optionsInfo:(NSArray *)optionsViewArray
{
//IT NEVER REACHES HERE :(
NSLog(#"HERE");
self.infoArray = optionsViewArray;
NSLog(#"Finished");
[self dismissViewControllerAnimated:YES completion:nil];
}
Has any one has done something similar like this or knows the solution to this? Any information, link, guidance and etc. to the right direction will be appreciated. Thanks in advance.
You need to set the delegate in your OptionsViewController below:-
In your OptionsViewController.m Include below line on your method
[self setDelegate:ModalViewController];

Unable to set custom protocol delegate using ARC with two UITableViewControllers using UINavigationController

I'm trying to set the delegate for my custom protocol that has one required method allowing me to pass an array of objects back in the hierarchy of two UITableViewControllers. My delegate continues to return nil. Due to this, my required method is never called.
I'm wondering if the datasource and delegate implementations with my UITableViewControllers is causing a conflict. Also, perhaps ARC is getting in the way when declaring the delegate?
It should be noted that both UITableViewControllers were built using Storyboard and are navigated using segues within a UINavigationController (not sure if this may be causing issues or not).
The nav is --> AlarmViewController --> AlarmDetailsViewController. I create an Alarm object in my AlarmDetailsViewController that contains all the details for an alarm, place it into an array and I want to pass that array back to my AlarmViewController to be displayed in a custom cell in the table.
NOTE: I want to use the Delegate pattern here. I'm not interested in solutions that invoke NSNotifications or use my AppDelegate class.
AlarmDetailsViewController.h
#import "Alarm.h"
#protocol PassAlarmArray <NSObject>
#required
-(void) passAlarmsArray:(NSMutableArray *)theAlarmsArray;
#end
#interface AlarmDetailsViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
{
//.....
id <PassAlarmArray> passAlarmsArrayDelegate;
}
#property (nonatomic, retain) id <PassAlarmArray> passAlarmsArrayDelegate;
#end
AlarmDetailsViewController.m
#import "AlarmDetailsViewController.h"
#interface AlarmDetailsViewController ()
#end
#implementation AlarmDetailsViewController
#synthesize passAlarmsArrayDelegate;
-(void) viewWillDisappear:(BOOL)animated
{
NSLog(#"delegate = %#", self.passAlarmsArrayDelegate); // This prints nil
[[self passAlarmsArrayDelegate] passAlarmsArray:alarmsArray];
}
//....
#end
AlarmViewController.h
#interface AlarmViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate, PassAlarmArray>
{
//...
AlarmDetailsViewController *alarmDetailsViewController;
}
#property (nonatomic, retain) AlarmDetailsViewController *alarmDetailsViewController;
#end
AlarmViewController.m
#import "AlarmViewController.h"
#import "AlarmDetailsViewController.h"
#import "AlarmTableViewCell.h"
#import "Alarm.h"
#interface AlarmViewController ()
#end
#implementation AlarmViewController
#synthesize alarmDetailsViewController;
- (void)viewDidLoad
{
[super viewDidLoad];
// This is where I'm attempting to set the delegate
alarmDetailsViewController = [[AlarmDetailsViewController alloc]init];
[alarmDetailsViewController setPassAlarmsArrayDelegate:self];
}
//....
//My #required protocol method which never gets called since my delegate is nil
-(void) passAlarmsArray:(NSMutableArray *)theAlarmsArray
{
alarmsTableArray = theAlarmsArray;
NSLog(#"alarmsTableArray contains: %#", alarmsTableArray); // Never gets called due to delegate being nil
NSLog(#"theAlarmsArray contains: %#", theAlarmsArray); // Never gets called due to delegate being nil
}
#end
I've attempted to set the delegate in a method that fires when a button is pressed in AlarmViewController (as opposed to the viewDidLoad method) but that does not work either.
I'm assuming I've got a logic flow error somewhere here . . . but nearly 2 days of hunting and rebuilds haven't uncovered it. Ugh.
You're setting your delegate in the wrong place, and on a different instance of the controller than the one you will get when you do the segue. You should set the delegate in the prepareForSegue method if you're pushing AlarmDetailsViewController from AlarmViewController
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
AlarmDetailsViewController *alarm = segue.destinationViewController;
alarm.passAlarmsArrayDelegate = self;
}
You really need to understand the life cycle of view controllers, how and when they're instantiated, and when they go away. This is the very heart of iOS programming, and Apple has extensive documentation on it. Reading up on segues would also be very useful. A segue (other then an unwind segue) always instantiates a new instance of the destination controller. So, when your segue is performed, whether directly from a button, or in code, a new (different from the one you alloc init'd directly) details controller is instantiated. Before that segue is performed, prepareForSegue: is called, and that's when you have access to the one about to be created. That's the place to set a delegate or pass any information on to the destination view controller.
Did you try replace (nonatomic, retain) with (nonatomic, strong) since you are using ARC?
Auto-synthesized properties like your alarmDetailsViewController property have backing ivars prefixed with underscores, e.g. _alarmDetailsViewController. Your alarmDetailsViewController ivar (the alarmDetailsViewController declared inside the #interface ... {} block in AlarmViewController.h) is different from the backing ivar of your alarmDetailsViewController property.
Just delete your alarmDetailsViewController ivar and use the #property, preferably through self.alarmDetailsViewController.

Resources