I am using a UITableView within a UIPopover with the aim to set a property to an instantiated object i am using within the original ViewController class(from where the popover is launched). However I cant get access to the declared object(from within the ViewController class). I have tried to import the ViewController class to the popover class, but to no avail, the object just isn't visible within the Popover class. Any guidance on this would be greatly appreciated.
Your popover controller is just a wrapper to place a normal UIViewController in to give it that particular effect. Therefore, to get your variable in scope in the popover controller, you need to set up your UIViewController subclass with a property.
#property (nonatomic, retain) Thing *thing;
So your Main view which is calling the popover will initialize the UIViewController with the property above, and pass it into the init method for the UIPopoverController and proceed as normal.
Related
Would appreciate any advice as I try and wrap my head around this - I'm not sure if I'm implementing this wrong, or am working from the wrong premise (or both).
If I have a class in which I've created a protocol (the delegatOR) - in order to assign a delegate for that protocol, am I right to say I need to alloc/init the 'delegatee' class somewhere in the delegator's implementation file, and then assign it as the delegator's delegate?
If so, follow up question: I have a tabBarController set up in Storyboard, and when the user clicks on the 'end' tab I'd like to send a message to the viewController for the view they're about to leave, so it pops up an alert saying something like 'are you sure?'. Since storyboard does the initializing and allocating behind the scenes, I'm at a loss as to how to set up the delegate. I read in another posting about using the prepareForSegue method when segueing between two viewControllers to set the delegate, but can't work out a similar catch-and-set technique for the tabBarController.
If I have a class in which I've created a protocol (the delegatOR) - in order to assign a delegate for that protocol, am I right to say I need to alloc/init the 'delegatee' class somewhere in the delegator's implementation file, and then assign it as the delegator's delegate?
No. The way it usually works is that some other class, usually some kind of controller, will instantiate (i.e. alloc/init) an object as well as its delegate, and set the object's delegate.
For example, if you have a UITableView and a UITableViewDelegate, typically you have some kind of view controller that owns the UITableView, and also owns the UITableViewDelegate (often the view controller itself is the delegate). It's the view controller's job to set the table view's delegate. It most certainly is not the UITableView's job to create and set its delegate.
Still, you're right that setting delegates for view controllers can be hard when you're using Storyboard. If the prepareForSegue: method isn't working for you, I assume it's because the tab bar controller is your root view controller (as it well should be, according to the HIG). Since your app delegate will have a window property, you should be able to get to the root view controller from it:
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;
tabController.selectedIndex = [defaults integerForKey:kOptionLastTabSelectedKey];
tabController.delegate = self;
So I have a UIViewController A which adds a UIView B as a subclass. The UIView B has a UITableView. I want the UITableView scrollView delegate to be in the UIViewController A. How do I do this? As of now the scrollViewDidScroll delegate is inside this UIView class. Is there a way so that the scrollViewDidScroll is inside viewController A and is called whenever the UITableView in the UIView is scrolled?
Make the tableView as a property accessible from the outside. Than you could set it up in your ViewController A like that:
// ClassB.h
#property (nonatomic, readonly) UITableView* tableView;
// In your UIViewControllerA.m
// in loadView: or anywhere else
self.viewB.tableView.delegate = self;
So result is: your ViewController is the delegate.
The scroll view and the table view are the same object -- notice that UITableView is a subclass of UIScrollView. That object has only one delegate, not separate delegates for the table stuff and for the scroll stuff. Also notice that UITableViewDelegate adopts UIScrollViewDelegate. So, the object you set as the table's delegate will also get UIScrollViewDelegate messages.
Now, there's no reason that your table's delegate can't forward messages about scrolling to some other object. You'd have to set that up yourself, of course. So, when B gets a -scrollViewDidScroll: message, it might send an equivalent message to A, or whatever. I'd think twice before actually doing that, though... I'd try to have just one object (the view controller) be responsible for everything related to the table.
I know this has been discussed a number of times but I still have some problems getting around the problem, so any help would be appreciated. I have a class MyView which subclasses UIView. So far so good, in this custom class I basically configure the layout, UI, etc.
Now, I have a separate UIViewController and what I want to do is create an outlet for that custom view. In the view controller's Nib I set the view's class to that of my custom view and connect it to the outlet, but I can't see anything apart from a blank view.
How and where do I load the view from nib? Do I simply say self.theOutletForMyCustomView = load from nib or is it something else? Thanks for your help
First of all, you have to set the name of your CustomView inside your UIViewController nib file like that
Then, you have to retain your property like that inside your UIViewController interface :
#property (nonatomic, retain) IBOutlet CustomView *myCustomView;
or an ivar should work, but assign an IBOutlet property doesn't work.
And if you customize your CustomView inside your CustomView class implementation. Beware of doing your initialization in awakeFromNib or initWithCoder: instead of initWithFrame:
I am in the learning phase of iOS programming. I am trying to add a NIB (called B) in another NIB (called A). To do so, I have added a View Controller in A which uses the B NIB (using NIB Name). In addition, in the A's ViewController, I have created an IBOutlet to store B's ViewController. Finally, in A I have connected the IBOutlet to the ViewController referring to B.
Now, I expect that when I run the project, I would see that B is loaded whenever A is loaded, but that is not the case. To achieve this, I have to initialize B pragmatically using initWithNibName in A's viewDidLoad method, and set it to the IBOutlet. But this part is something that is not dependent of using both IBOutlet in A and adding a ViewController to A that refers to B. In other words, if I had simply initialized B in A's viewDidLoad without creating an IBOutlet in A and without creating a View Controller object in A, that would have worked just as well.
So my question is, what is the purpose of using IBOutlet for adding custom View Controller NIBs?
Just because you have outlet to VC B in VC A, that does not mean view of VC B will be loaded automatically. If you check VC B outlet in VC A viewDidLoad, you'll see there is address to VC B, therefore NIB is loaded. To load its view, you have to access VCB.view and call appropriate methods in VCB like this:
[self.bViewController setFrame:[self frame]];
[self.bViewController viewDidLoad];
[self.bViewController viewWillAppear:NO];
[self.view addSubview:self.bViewController.view];
[self.bViewController viewDidAppear:NO];
You dont need to do all that, and adding another viewcontroller view in currently displayed view controller view hierarchy causes problems like interface rotation or touch events do not delivered properly.
You can use uiviewcontroller's presentViewController:animated:completion: method to load bViewController's view hierarchy and all the methods like viewDidAppear, viewDidLoad, viewWillAppear will be called automatically you do not need to call them manually.
You will find this article Abusing UIViewControllers useful.
In iOS4, I want to use MPMoviePlayerController. I have a UIViewController that I pass to a custom class that manages MPMoviePlayerController. That plays video fine.
I have another view that is a UITableViewController. Passing the UITableViewController to my UIViewController property on the video class doesn't work. I just get sound. I'm fairly sure this is because the UITableViewController needs to be a UIViewController. I can change the UITableViewController to a UIViewController but this is a fairly good rewrite. Is there a way to access the parent UIViewController behind the UITableViewController?
I have also tried
(UIViewController*)self;
but that doesn't work either. I just get sound again.
You can use `self.parentViewController. From the documentation:
Parent view controllers are relevant
in navigation, tab bar, and modal view
controller hierarchies. In each of
these hierarchies, the parent is the
object responsible for displaying the
current view controller. If you are
using a view controller as a
standalone object—that is, not as part
of a view controller hierarchy—the
value in this property is nil.