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

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.

Related

Warning when setting a delegate

I'm trying to pass data back with the segue, and am following this answer: How to Pass information Back in iOS when reversing a Segue?
However when I try to put this line in:
#property (nonatomic) id<MyDataDelegate> delegate;
I get the following warning:
And when I try to put to the first view controller like this:
#interface ContainerViewController : UIViewController <MyDataDelegate>
I get the error "Cannot find protocol declaration for 'MyDataDelegate' " and I did include the other header file...
From the error message it looks like you are adding the property to a subclass of UIPageViewController. But UIPageViewController already has a delegate property of a different type.
You therefore need to either rename your property to something else, or declare MyDataDelegate to conform to UIPageViewControllerDelegate so that your property redeclaration is compatible with the base class version:
#protocol MyDataDelegate <UIPageViewControllerDelegate>
...
#end
You also need to declare your property with the weak attribute.

Call a function from a subview UIViewController Xcode

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

iphone - delegates

I have an object that belongs to a class. Lets call it classA. This classA is a subclass of classB.
ClassA has a delegate protocol.
I create an classA object on my main code. This object is inside a view. Lets call it viewX.
Now I am in classB and I would like to get a reference to viewX.
remember that classA has a delegate protocol, so it has a reference to its delegate, that is the viewController where viewX is. From class A I can access viewX doing [delegate view], but how do I do that from classB???
thanks.
If you have access to class B, I would say you should add a variable to the class of type id, and set that variable as the view, and that would be a very easy way to do it. Otherwise, I don't think it's possible. But I may be wrong.

How can you share ivars between classes.?

In one classes .h i have
NSMutableArray *rountines;
and in another classes .m i want to do something like this
[routines addOject:#"Hello];
the other class is an ModalViewController type set up.
So, in essence i'd like the .m file of the MVC to be able to read, and edit and do other things to the ivars i declare in the header.
Cheers,
Sam
edit
Another example, which is similar to what im trying to achieve is like an edit screen.
You can't share ivars between classes really. ivar stands for instance variable, and that is a variable that belongs to some particular instance of an object. The way to solve this problem is to allow other objects to access this object's state. This is most commonly done through setter and getter methods. Objective-C 2.0 makes this easier by providing the #property and #synthesize keywords.
If you had access to the object that had the routines array, you could access it through its property (getter method) like this:
[[someObject routines] addObject:#"hello"];
you can either do this by making the ivars you want to share globals (in which case they would be ivars of the singleton class or app delegate class) or by passing a reference to the class you want to modify the ivars of as an argument to a method of the ModalViewController class:
#implementation ModalViewController
......
-(void)addObjectToRoutinesFromClass: (MyClass *)myclass {
[myclass.routines addObject:#"Hello"];
}
#implementation MyClass
......
ModalViewController *myModalViewController = [[ModalViewController alloc] init];
[myModalViewController addObjectToRoutinesFromClass:self];
#end

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