I'm trying to send an array from one viewController to another using protocols and delegates. I declared the delegate on viewcontroller B and used the following code in viewcontroller A to send the message from A to B. The protocol's method is didReceiveMessage. Unfortunately, the message never arrives.
Attached is the code from viewController A
- (IBAction) graphPressed:(UIButton *)sender {
GraphingViewController *gvc=[[GraphingViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:gvc animated:YES];
[gvc release];
[delegate didReceiveMessage:brain.internalExpression];
}
and the code from viewcontrollerB
- (IBAction) ViewdidLoad {
CalculatorViewController *cvc =[[CalculatorViewController alloc] init];
cvc.delegate=self;
[cvc release];
}
- (void) didReceiveMessage:(NSMutableArray *)expression {
NSLog(#"message received from CalculatorAppDelegate");
}
Any suggestions would be greatly appreciated.
I'm not sure what are you doing in second sample? You created an object, assign its delegate property and then released it. Why?
If that is code from your application it could probably be from releasing cvc at the end of your ViewDidLoad method.
init would give it a retain count of 1, and the release would take it back to 0.
Also the code seems sort of backwards, if you wanted to set view A as the delegate for view B, you would do so in view A.
Unless there is a more complex reason to use a delegate that I'm not seeing from the code, I would just keep a pointer around to the child if you really need to send it messages.
Like others have posted, you are just getting rid of the Calculator VC after creating it. I would recommend adding an #property to the second VC and using it to store a pointer to the Calculator. It should be a retain property. Then set the delegate of that property to self.
Also, you make sure to use an assign property for the delegate value, and try to use the property syntax (self.delegate) whenever possible.
There could absolutely be more going on here, so if this doesn't actually solve the problem try and post up more of your code (header files, what connections are made in IB, etc.)
Edit: For the record, the method is -(void)viewDidLoad, not -(void)ViewDidLoad, so that could be contributing to the problem.
As you said you are trying to pass an array from one view controller to another.Well i use in this way.Here is the code
- (IBAction) graphPressed:(UIButton *)sender {
GraphingViewController *gvc=[[GraphingViewController alloc] initWithNibName:nil bundle:nil];
gvc.myArray=infoArray;
[self presentModalViewController:gvc animated:YES];
[gvc release];
}
Where myArray is array in your GraphingViewController,You just need to make property of this array with retain attribute,simply as
#property(nonatomic,retain)NSMutableArray *myArray;
And you need to Synthesize it as well.
Related
I was trying to push a viewcontroller B into navigation controller from A and then assigning some properties of B in A.
In this case, the assigning of properties was done and then viewDidLoad of viewcontroller A was executed.
Here, assigning properties in A should be done only after viewDidLoad of A has done.
For example,
[b.navController pushViewController:a animated:YES];
a.status = #"loaded";
Here, status was assigned first and then viewDidLoad of A was executed.
This happens only in iOS 7 whereas in iOS6 it works fine.
Can anyone please let me know where the problem is?
UPDATE: For me in some cases in iOS7, Push view is not working. How cna I debug and fix it?
Just access the viewcontroller.view (set any thing immediately after the alloc) property after the alloc init;
Which will loadview/viewdidload.
See Apple Documentation
In my experience, a UIViewController view is loaded lazily, no matter which iOS version you're working on. If you want to trigger a view load, and therefore its UIViewController viewDidLoad, you should access the view after the UIViewController is allocated. For example:
UIViewController *aViewController = [[CustomViewController alloc] init];
[aViewController view];
Make sure you don't code it as
aViewController.view
since that would generate a compiler warning.
So, in your case it would have to be something like this:
...
CustomViewController *a = [[CustomViewController alloc] init];
[b.navController pushViewController:a animated:YES];
[a view];
a.status = #"loaded";
Let me know if you have further problems with it.
You can know when a View Controller has been pushed onto the stack by implementing the UINavigationControllerDelegate and setting yourself as the delegate self.navigationController.delegate = self; then you will get this callback after every push
navigationController:didShowViewController:animated:
So you can check if the shown viewController is the one your interested in, then set your a.status.
I would suggest you call a delegate method once the view is loaded.
Set the delegate to be controller B.
and after viewDidLoad finishes (in controller A) call the delegate method. You can even pass parameters as you wish to the delegate.
Here's some example code:
Controller B:
a.delegate = self;
[b.navigationController pushViewController:a animated:YES];
Implement the delegate method:
- (void)controllerIsLoaded:(ControllerA *)controllerA status:(NSString *)status
{
a.status = status;
}
Controller A .h file:
#class ControllerA;
#protocol ControllerADelegate <NSObject>
- (void)controllerIsLoaded:(ControllerA *)controllerA status:(NSString *)status;
#end
#interface ControllerA : UIViewController
#property (nonatomic, weak) id <ControllerADelegate> delegate;
Controller A .m file:
- (void)viewDidLoad:(BOOL)animated
{
[super viewDidLoad:animated];
/* your viewDidLoad code here... */
if ([_delegate respondsToSelector:#selector(controllerIsLoaded:status)])
[_delegate controllerIsLoaded:self status:#"Loaded"];
}
Turn off animation for ios7, in my case its work
[b.navController pushViewController:a animated:NO];
a.status = #"loaded";
No documentation provides enough information to know exactly when viewDidLoad would be called.
UIViewController's documentation just says this
This method is called after the view controller has loaded its view hierarchy into memory
I would suggest that you create a custom initializer like this
- (id)initWithStatus:(NSString *)status {
}
But, if you are trying to use this variable to check if the viewController's view has 'loaded' or not, it may not be possible to do that because the pushViewController or presentViewController methods are not guaranteed to be synchronous.
Even in iOS 6, there was no explicit guarantee that the view would be loaded as soon as that method returned.
Please write the code in viewWillAppear method instead of viewDidLoad in next class i.e. where you are pushing the object to
-(void)viewWillAppear:(BOOL)animated
{
}
I'm understand of your question like this:
B *b = [[B alloc] init];
b.status = #"loaded";
[self.navigationController pushViewController:b animated:Yes];
If you want to pass a value from one controller to another means, you have to assign a value before using pushViewController method.
I have two view Controllers in my project ViewController, SettingsView. Here I am trying to update the ViewController's label, when i click on the SettingsView's back button. NSLog is working fine, but the label is not updating...
Please help me....
SettingsView.m
-(IBAction)backToMain:(id) sender {
//calling update function from ViewController
ViewController * vc = [[ViewController alloc]init];
[vc updateLabel];
[vc release];
//close the SettingsView
[self dismissModalViewControllerAnimated:YES];
}
ViewController.m
- (void)updateLabel
{
NSLog(#"Iam inside updateLabel");
self.myLabel.text = #"test";
}
Could you please tell me whats wrong with my code? Thank you!
You have to implement protocols for that. Follow this:
1) In SettingView.h define protocol like this
#protocol ViewControllerDelegate
-(void) updateLabel;
#end
2) Define property in .h class and synthesis in .m class..
#property (nonatomic, retain) id <ViewControllerDelegate> viewControllerDelegate;
3) In SettingsView.m IBAction
-(IBAction)backToMain:(id) sender
{
[viewControllerDelegate updateLabel];
}
4) In ViewController.h adopt protocol like this
#interface ViewController<ViewControllerDelegate>
5) In viewController.m include this line in viewDidLoad
settingView.viewControllerDelegate=self
Your label is not updating because , you are trying to call updateLabel method with a new instance.
You should call updateLabel of the original instance of viewcontroller from which you have presented your modal view.
you can use a delegate mechansim or NSNotification to do the same.
Delegate mechnaism would be clean. NSNotification is quick and dirty.
You are not exactly calling the correct vc. This is because you are creating a new instance of that class and calling the updateLabel of that instance.
You have a few options.
Either implement it as a delegate callBack (delegate messagePassing, or delegate notification - however you want to call it) to notify that class instance to call the updateLabel method.
Use the original instance VC as a dependency injection into the class that you are on right now, and use that instance to call the updateLabel
Use NSNotifications / NSUserDefaults to communicate between viewControllers and setup a notification system for your actions. This is quite easy, but not really great in the long run.
I would RECOMMEND option 1 (or) option 2.
Simply declare like this in SettingsView class:
UILabel *lblInSettings;// and synthesize it
Now assign like below when you presenting Settings viewController:
settingsVC.lblInSettings=self.myLabel;
Then whatever you update in lblInSettings it will be present in MainView obviously....
no need for any delegate methods or updating methods.
Means if you assign at the time of dismissing like
lblInSettings.text=#"My new value";
then self.myLabel also will be updated.
Let me know if you have any queries?
In my app there are two tabbars. In tab-0 parent class is "FirstViewController" and in tab-1 parent class is "SecondViewController". In "SecondViewController" i have declared protocol and custom delegate method. i want to pass the information in "FirstViewController"(FVC). So FVC has to assigned as a delegate.
Now my doubt is, right now i am in "SVC". How can i assign "FVC" as a delegate of "SVC"?
In "SVC"
[[self delegate] sendCoordinates:self];
Definition of method is in "FVC". To execute this method, first i need to assign "FVC" as a delegate.
I hope I am clear in explaining my problem.
Thanks in advance.
You need to set the delegate. Let me demonstrate:
`SVC.h`
#interface SVC
{
id _delegate;
}
- (void)setDelegate:(id)delegate; //sets the delegate
- (id)delegate; //gets the delegate
#end
#protocol SVCDelegate
- (void)sendCoordinates:(SVC *)svc;
#end
Then, in SVC.m, you call the delegate in exactly the same way as you showed in your question, so [[self delegate] sendCoordinates:self];
Now, in FVC, you'll need to #import SVC.h and initialise the object.
SVC*svcObject = [[SVC alloc] init];
[svcObject setDelegate:self]; //self now refers to FVC - I think you're missing this one
//work with the object and when you're done, get rid of it in dealloc:
[svcObject setDelegate:nil];
[svcObject release];
In the same class, implement - (void)sendCoordinates:(SVC *)svc and it will be called after you set the delegate.
I think you're missing the setDelegate: stage, which is why it doesn't work.
Hope it helps!
Note: In SVC, remember to retain the delegate, or it will become nil and no delegate methods will never be called. Don't forget to release that delegate once you're done.
here is my navigation on my app
1) homescreenview controller-->composemessageviewcontroller (i am able to use delegate to send data back to homescreenview)
2)homescreenview controller -->messageslistcontroller(tableview)-->detailmessageviewcontroller(which is where my reply button is).
my problem is when i hit reply i want to send back information to homescreenviewcontroller with delegate . how can i do this?
Thanks in advance.
----UPDATE
#XJones, thanks for the detailed explanaion. is this what is should be doing in when i push detailview? please correct me if i am wrong.
(void)pushDetailMessageController{
DetailMessagetController *detailmessage = [[DetailMessagetController alloc] init];
detailmessage.delegate = self;
// push messageListController onto navigation controller here
[detail release];
}
that's a very general question. you're basically asking how to pass information from one controller to another controller. There are different ways to do that, a protocol (what a delegate generally communicates through) is one of them. The quickest thing you can do w/o making assumptions in your code that may be problematic later would be to pass the homeScreenController along as you push messageListController and then detailMessageController. You'll need to define an iVar and property in messageListController and detailMessageController to do this.
Something like:
in messageListController.h:
#import "HomeScreenController.h"
#interface messageListController : UITableViewController {
// your iVars
HomeScreenController *homeScreenController;
#end
#property (nonatomic, assign) HomeScreenController * homeScreenController;
add the same iVar and property for homeScreenController to detailMessageController.
in homeScreenController.h:
- (void)pushMessageListController
{
MessageListController *messageListController = [[MessageListController alloc] init];
messageListController.homeScreenController = self;
// push messageListController onto navigation controller here
[messageListController release];
}
in messageListController do the same thing as above when creating and pushing detailMessageController. Now, in detailMessageController you can send messages directly to homeScreenController.
If you want to generalize the above implementation so your controllers aren't specifically knowledgeable about each other then you can define a protocol and pass homeScreenController through as a delegate supporting that protocol.
How about adding a method in the messageslistcontroller? I personally would add the delegate "homescreenview" to the detailmessageviewcontroller as the messageslistcontroller doesn't have anything to do with the reply and apperantly the homescreenviewcontroller does.
when you create the detailmessageviewcontroller in the messageslistcontroller do this:
detailmessageviewcontroller.homeScreenDelegate = self.delegate;
One approach (without delegation)
as you are using navigationController, so
[[self.navigationController viewControllers]objectAtIndex:0] will always return you homeScreenViewController.....you can use this object....
Thanks,
i am getting the web services from the .net web server.
while in the process (getting data) i am displaying a subview with activity indicator.
After completing getting data i need to close that view.
i have two classes one is myclassviewcontroller,webservices
Basically i am writing code to get web services webservices.
In webservices class at
-(void)connectionDidFinishLoading:(NSURLConnection *)connection i call myclass like this.
myclassviewcontroller *obj = [[myclassviewcontroller alloc]init];
[obj mymethod];
At myclassviewcontroller i write this code for my method.
(void)mymethod {
[loadview removeFromSuperview];
}
the method is executed but view is not removed.
I already declared it in myclassviewcontroller.h class also.
i am checking this by keeping some text in NSlog
But if i calling this mymethod in myclassviewcontroller.m using timer then it removes view.
what the wrong.
can any one please help me.
I think it may be understand what is my problem.Let me place comment if not.
Thank u in advance.
I believe the problem with your code is how you access the myclassviewcontroller. It must have already been on the screen while the data was loading, so creating a new instance of that class and calling a method against one of it's uninitialized members (loadview) does nothing.
myclassviewcontroller *obj = [[myclassviewcontroller alloc]init];
// here object has just been initialized
// (it is not the same instance as the one on screen)
[obj mymethod];
If obj was a reference to the actual viewcontroller that is on screen, you could easily call:
[obj.loadview removeFromSuperview];
or
[obj mymethod]; // if you wanted to add more code in that function
So, the real problem is that you accessing a different instance of myclassviewcontroller than the one which is actually on screen. You need a variable holding some reference to the correct instance of myclassviewcontroller to access the loadview ivar.
In webservices.h:
#interface webservices : NSObject {
...
// This ivar will have to be set when webservices is initialized
myclassviewcontroller * viewController;
}
#property (nonatomic, retain) myclassviewcontroller * viewController;
and webservices.m would need to #synchronize viewController.
Then in connectionDidFinishLoading: you can just call [viewController.loadview removeFromSuperview];
the problem could be that you instantiate your myclassviewcontroller when "loadview" is already allocated by your "main" class but "invisible" in your myclassviewcontroller, so your new instance of myclassviewcontroller doesn't really know who "loadview" is...
i mean: loadview is allocated and added to the mainView (in the same class where you allocate
"myclassviewcontroller"...)
but then you try to remove it not in your mainView, but in myclassviewcontroller...
try to modify your method this way:
(void)mymethod {
if (loadview!=nil){
NSLog(#"I'm here...");
[loadview removeFromSuperview];
}
}
to see if "loadview" exist when and WHERE you call the method (in myclassviewcontroller)
luca