Call a function from a subview UIViewController Xcode - iphone

I have ADMINviewController that contains a UITableView and contains a function called Request that loads the devices from the server and fill the UITableView, when I select a cell, which is a device name, VehicleInfoViewController as a subview opens to let me change the device name.
So what I want is to call the Request function after showing a message that the device name has changed successfully, in order to reload the data from the server and update the UITableView with the new device name.
How to call the Request function from the subview?

If you want to call Request method which is in ADMINviewController class from VehicleInfoViewController class you should pass a pointer of ADMINviewController instance to VehicleInfoViewController.
Add id property to VehicleInfoViewController class like this:
#property(nonatomic,assign)id adminController;
Then after you create VehicleInfoViewController instance, pass self pointer to it like this:
vehicleInfoViewController = [[VehicleInfoViewController alloc] initWithNibName:#"vehicleInfoViewController" bundle:nil];
vehicleInfoViewController.adminController = self;
When you need to call Request method being inside vehicleInfoViewController do this:
[self.adminController performSelector:#selector(Request)];

The approach above (by Rafael) can also be used by a subView to call a method located in that subView's viewController. Just make sure to add the necessary methods to avoid compiler warnings, e.g., use
#synthesize adminController
in the implementation of the VehicleInfoViewController class for the above example. This can also be called from within any object that has a VehicleInfoViewController instance. For example, assuming parentObject contains a vehicleInfoViewController member variable, calling Request within the parentObject would look like:
[parentObject.adminController performSelector:#selector(Request)];

Related

What would be the appropriate way to use a delegate protocol from 2 views?

I have a view controller, which uses a delegate protocol in order to pass back an array of strings. I now have another view controller, which I'd like to use the same protocol, but I've I use it I get a warning in Xcode Duplicate protocol definition of 'SearchDetailsDelegate' is ignored.
I need these two views to pass back an array for the parent view controller to parse. What would be a more appropriate way of achieve what I need to do here? Would key value observing be the way to go here?
You have few options:
rename your protocols to be different.
create an external protocol and adopt that protocol on each view
Add a property to your view called ParentController with a type of it's parent.
#property (strong,nonatomic) ParentViewController *ParentController;
(synthesise that off course)
Then, in your viewController, when you instantiate the view assign the viewController as the parent
YourView *childView = [[YourView alloc]init];
childView.parentController = self;
Now you can add a method in your viewController that can receive the strings array
-(void)setStringsArray:(NSArray*)arr{
//do what ever you need with the array
//don't forget to add this method to your .h file so it will be visible
}
Lastly send the strings array from the view:
[self.parentController setStringsArray:yourArray];
BTW
if you want to know what view send the array you can:
-(void)setStringsArray:(NSArray*)arr fromView:(UIView*)senderView{
//do what ever you need with the array
//don't forget to add this method to your .h file so it will be visible
}
and use
[self.parentController setStringsArray:yourArray fromView:self];
BTW 2
an other option will be to use notifications.
Define the protocol in a separate .h file (new file of objective c protocol) and then include it in the required view controllers.Redefining the same protocol in two different view controllers is not recommended as it has been in your case

How to pass change the value of a variable in another view(objective c)?

i have two controller
in view1.h i have
NSString *bg;
#interface Lottery5ViewController : UIViewController {...}
in view2.m i have
-(IBAction) switchView2{
self.bg = #"blueButton.png";
}
but it cannot compile and said
request for member 'bg' in something not a structure or union
i just want to modify the value of a variable in view1 from view2
Your problem is that in view2's switchView2 method you are calling self and trying to change an ivar (or property, not sure which it is from the code shown) of a different class. If you want to send something from one instance to another you have to establish some sort of connection between them first. There are a few ways you can get them talking. The better ways to be able to pass something like this is to set up a delegate protocol or use notifications, then view2 could call the delegate method that makes the change in view1 or post a notification that view1 would be registered for so that it could make the change. The less desirable way to accomplish this would be to give view2 an ivar (or property) that is view1. You would then be able to use something like view1.bg = #"blueButton.png";.

Can't determine calling class's variable's value using XCode

Here's what I'm looking to do. I have a UITableViewController that has a variable, bInternetOK, that I need to reference from a class that the UITableViewController instantiates. This way, the class can make sure the internet is available before it tries to do some work. In the .h of the UITableViewController, I've defined the variable like this:
Boolean bInternetOK;
And I've set a property like so:
#property (nonatomic) Boolean bInternetOK;
In the .m of the UITableViewController, I've synthesised the variable like this:
#synthesize bInternetOK;
I instantiate the class (TheNetworkClass) like this and then call the function to start the work:
TheNetworkClass *TheNetworks = [[TheNetworkClass alloc]init];
[TheNetworks StartUpTheWork];
Inside of the TheNetworkClass class, I'm trying to reference the variable bInternetOK that is in the UITableViewController class. How do I do this?
Thanks!
You could give the instance of your TheNetworkClass object a reference to your table view controller instance when you create it.
Or you could just give your TheNetworkClass object the boolean, if it only checks it on creation.
Or, if the table view can be accessed as a property on your application delegate, you can get the application delegate with [UIApplication sharedApplication].delegate and then access that property to get to the table view.

passing array from one view to another

i am a new iphone programmer i want to create an array that keeps adding a value at its last position when i moving from one view to another
because at last view ,i need that full array which has all values from all views....
i tried taking an array and sending it view to next view (array is defined in all views) but when i tried to put value to it
You can use NSMutableArray class with addObject: in order to add your views into array. Have a single view controller, declare that array and all your views can access the view controller's array.
try to save it to a class (singelton class) it will work....it will surely work if you transfer it to the delegate class....make an NSMutableArray object there and initialize it once in applicationDidFinishLaunching method and then make object of delegate class and transfer your values to it by addObject method.....
The best way is initialize your mutablearray in appdelegate and use the same using the object of delegate in each class wherever you want to use.you can add the object using addobject and can access using property objectAtIndex anywhere.

Creating an object to act as a delegate - Objective C

My question is simple actually, how do I create an object to act as a delegate, instead of including the delegate methods in my view?
For example, I have x functionality that requires delegate methods, and they're currently setup to use self as the delegate. I'd like to put those methods in their own object so that the delegate methods can be called and do stuff if the view has ended.
What's the best way?
for example, NSXMLParser delegate methods - they exist, the delegate is defined, but I dont want to call them as self in my view controller... what other option do I have?
You can specify another custom class to handle the delegate methods, if you wish. Simply create a class, call it MyXMLParserDelegate or something similar. Then, all you have to do is tell your NSXMLParser object that it should use an instance of your class as its delegate.
If you are using Interface Builder, add a new object to the XIB file, set its class to MyXMLParserDelegate, and then drag a connection from your NSXMLParser object's delegate selector to the new object.
If you are doing it programmatically, the basic operation looks like this:
MyXMLParserDelegate * myDelegate = [[MyXMLParserDelegate alloc] init];
[someXMLParser setDelegate:myDelegate];
Keep in mind, however, that delegates are not retained, so in order to do this without leaking memory, you should add an ivar of type MyXMLParserDelegate to your viewController class, and then do the following:
// in your #interface block:
{
...
MyXMLParserDelegate * myDelegate;
}
// in your init method:
myDelegate = [[MyXMLParserDelegate alloc] init];
// in your awakeFromNib method (or anywhere else it seems appropriate):
[someXMLParser setDelegate:myDelegate];
// in your dealloc method:
[myDelegate release];
Check out this answer, I think it covers what you need: How to use custom delegates in Objective-C