Adding custom UIViewController with UITableView as a subview - objective-c

I'm trying to create custom UIViewController with a UITableView, load the UIViewController using a xib file and add the view as a subview to another UIView.
The hierarchy is like this:
UIViewController
UIView << add custom UIViewController's view
UIView
UIView
Here's my xib view hierarchy and settings:
UIView
UITableView
Connection in IB:
File's Owner:CustomTableViewController
Outlets:
view connected to UIView
tableView connected to File's Owner
delegate connected to File's Owner
datasource connected to File's Owner
I have both UITableDataSource and UITableDelegate implemented.
When i tried to add the view as a subview, it crashed ...
- (void)viewDidLoad
{
[super viewDidLoad];
CustomTableViewController* controller = [[CustomTableViewController alloc] initWithNibName:#"CustomTableView" bundle:[NSBundle mainBundle]];
[self.viewContainer addSubview:controller.view];
}
What am i missing?

Sounds Like something is not retained that should be. Set up an exception breakpoint and turn on zombies to find it. See askers results above.

Related

Loading UITableView to a View in another ViewController

In my storyboard file I have 2 viewcontrollers. A UITableViewController and a ViewController.
I've completed all the needed coding for both of them. But I need to load the UITableView (and its data) inside a view in the ViewController.
Any suggestions?
Yes you can do this by adding UItableViewController as its ChildViewController like this
UItableViewController*vc1 = [[test1 alloc]initWithNibName:#"SecondViewController" bundle:nil];
//add to the container vc which is self
[self addChildViewController:vc1];
//the entry view (will be removed from it superview later by the api)
[self.view addSubview:vc1.view];
Swift:
let tableViewVC = UITableViewController(nibName: "SecondViewController", bundle: nil)
addChildViewController(tableViewVC)
view.addSubview(tableViewVC.view)
Hope it will help you.

How can a MPVolumeView be positioned?

I'm having trouble positioning a MPVolumeView. In the viewDidLoad method of my viewController I have this code:
[super viewDidLoad];
MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame: self.volumeView.bounds];
[self.volumeView addSubview: myVolumeView];
In the storyboard I've added the volumeView and I have hooked it up to the controller.
I'm including two screenshots: one of the storyboard and one of how the view looks when it is running on my phone.
How can I position the MPVolumeView in the volumeView?
The simplest way to do this is to create an MPVolumeView in Interface Builder instead of in code. You can do this by dragging a custom UIView into the XIB, configuring it as needed, and then changing its class to MPVolumeView.

Load In Custom View Xcode

This is for a Mac App and not for iPhone. What I want to do is take my current xib "Welcome Screen" and when it opens, load in another xib "WelcomeScreenText1" in a custom view called myTargetView. When it runs the error message is "[NSViewController loadView] loaded the "WelcomeScreenText1" xib but no view was set." Please help!
#import "WelcomeMainViewController.h"
#import "WelcomeText1ViewController.h"
#import "WelcomeText2ViewController.h"
#implementation WelcomeMainViewController
NSString *const text1Title = #"WelcomeScreenText1";
- (void)awakeFromNib
{
WelcomeText1ViewController* imageViewController =
[[WelcomeText1ViewController alloc] initWithNibName:text1Title bundle:nil];
if (imageViewController != nil)
{
myCurrentViewController = imageViewController;
}
// embed the current view to our host view
[myTargetView addSubview: [myCurrentViewController view]];
// make sure we automatically resize the controller's view to the current window size
[[myCurrentViewController view] setFrame: [myTargetView bounds]];
}
- (NSViewController*)viewController
{
return myCurrentViewController;
}
#end
The message means that the nib didn't connect anything to the view outlet of the view controller.
In your nib, make sure the custom class of File's Owner is set to WelcomeMainViewController. Then make sure that the view outlet of File's Owner is connected to the top-level view of the nib.

UIViewController with no Xib showing blank when using pushViewController

I have subclassed UIViewController like so:
#interface MyClass : UIViewController
I dont have a xib file for the controller, instead I would just like to use a blank UIView and I will layout elements programmatically on that. The problem arrises when I try and push this view controller.
MyClass * thing = [[ImageGallery alloc] init];
[self.navigationController pushViewController:thing animated:YES];
A new title bar is animated in, but there is no view, its see through, so I end up seeing a static background I set on my main "window/view". How should I properly subclass a UIViewController without a xib?
What you have described is correct behaviour so far.
You will want to override the -(void)loadView method, and after the [super loadView]; call, you can set your background colour and begin to place the objects in programatically, that you desire.

Why isn't my subview loading?

I have a project with 3 .xib files, MainMenu, FileUploadView, FileBrowseView.
MainMenu has a NSPanel, it's owner is AppDelegate, and AppDelegate has an outlet to NSPanel called FilePanel. The NSView below the NSPanel is called filePanelView and also has an outlet in AppDelegate.
FileUploadView is an NSView, it's owner is FileUploadViewController. It has an outlet called uploadView in the controller.
FileBrowseView is similar, owner is FileBrowseViewController, has an outlet called browseView.
So in App delegate I have the following code:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
fileBrowseViewController = [[FileBrowseViewController alloc]
initWithNibName:#"FileBrowseView" bundle:nil];
}
- (IBAction)importHandsClicked:(id)sender {
[NSApp activateIgnoringOtherApps:YES];
[filePanel setIsVisible:YES];
[filePanelView addSubview:[fileBrowseViewController browseView]];
}
The action does make filePanel visible, but it doesn't add the browseView to it. Am I doing something wrong?
Check that [fileBrowseViewController browserView] is not nil.
This is highly probably, especially if you forgot or failed to link your browseView IBOutlet to the actual UIView that represents your FileBrowseView instance in InterfaceBuilder.
[EDIT]
For more info about connecting outlets, see Apple's InterfaceBuilder Help here (including tutorial video).