I've been at this for a few hours now. I've got several View Controllers in this project and not a single one is causing issues but now all of a sudden this new one is. I even deleted it and made a "Test" View Controller, but no dice. The best I can tell it is not actually creating its view, thus when the view is referenced the app crashes. The test VC has no added or deleted code except for a log statement in the -viewDidLoad method. I am not overriding -loadView. I have tried adding the view to a subview, have tried pushing the VC into the Navigation Controller, I have even tried simply logging test.view. I have tried creating the VC with a NIB and have tried it without one. Absolutely nothing works at all. Any help will be appreciated.
Where VC is being created inside of another VC. The log statment causes the crash. But so does adding as a subview and even pushing into nav controller.
TestViewController *test = [[TestViewController alloc] init];
NSLog(#"test view = ", test.view);
Implementation of TestViewController.
#implementation TestViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"view = %#", self.view);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I'd say to double check all the connections from the view. Especially if its been copied or moved as its easy to leave a connection to an old VC
if you are not using ARC in your project then dont check the autolayout box for the nib file. If you are using ARC then the connections of it to the file's owner can be the only issue.
if you use xib,and the items is copied from other xib or storyboard ,make sure the root view is exist,and drag tothe file's owner.
Related
I am having a weird error with NSViewController where if I allocate a view using the viewcontroller's regular init message, the view created is not my view, but when using the default NIB name, it does work.
Specifically, this code works all the time. It creates the view defined in the nib file, and displays it in the parentView.
+ (void)createTransparentViewCenteredInView:(NSView*)parentView withText:(NSString*)text duration:(NSTimeInterval)duration {
TransparentAccessoryViewController* controller = [[TransparentAccessoryViewController alloc] initWithNibName:#"TransparentAccessoryViewController" bundle:nil];
NSLog(#"%#", [controller.view class]); // Returns "TransparentAccessoryView" -- CORRECT
[parentView addSubview:controller.view];
}
However, the following code works SOME of the time (which is weird in that it doesn't always fail). With some parentViews, it works perfectly fine, and with others, it doesn't. The parent views are just random custom NSViews.
+ (void)createTransparentViewCenteredInView:(NSView*)parentView withText:(NSString*)text duration:(NSTimeInterval)duration {
TransparentAccessoryViewController* controller = [TransparentAccessoryViewController new];
NSLog(#"%#", [controller.view class]); // Returns "NSSplitView" -- INCORRECT
[parentView addSubview:controller.view];
}
The errors that comes up are as follows (I have no idea why it is bringing up an NSTableView, as I don't have an NSTableView here at all. Also, it is weird that it complains about an NSTableView when the type it prints is an NSSplitView):
2013-04-07 21:33:12.384 Could not connect the action refresh: to
target of class TransparentAccessoryViewController
2013-04-07 21:33:12.384 Could not connect the action remove: to target
of class TransparentAccessoryViewController
2013-04-07 21:33:12.385 * Illegal NSTableView data source
(). Must implement
numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:
The NIB file defines a custom subclassed NSView, called TransparentAccessoryView, and hooks this up to the File Owner's view property, standard stuff (all I did was change the custom class name to TransparentAccessoryView). I added an NSLog's to see what was going on, and for some reason, in the second case, the view class type is incorrect and thinks it is an NSSplitView for some reason. The ViewController class is as follows:
#implementation TransparentAccessoryViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
- (void)awakeFromNib {
self.textField.stringValue = #"";
}
+ (void)createTransparentViewCenteredInView:(NSView*)parentView withText:(NSString*)text duration:(NSTimeInterval)duration {
TransparentAccessoryViewController* controller = [[TransparentAccessoryViewController alloc] initWithNibName:#"TransparentAccessoryViewController" bundle:nil];
NSLog(#"%#", [controller.view class]);
[parentView addSubview:controller.view];
}
#end
I thought that the default init message triggers the viewcontroller to load the NIB named after the viewcontroller, which seems to be the case some of the time as the second version of my code works in certain conditions.
Does anyone know why this behavior is occurring at all?
From the docs:
If you pass in a nil for nibNameOrNil then nibName will return nil and
loadView will throw an exception; in this case you must invoke
setView: before view is invoked, or override loadView.
Therefore, if you're initializing a NSViewController with -init, you should call -setView: to set the view controller's view, or override -loadView. In the latter case, you could certainly implement the UIViewController-like behavior that you're probably expecting -- if nibNameOrNil is nil, try to load a nib that has the same name as the class.
I think that when you call init on a NSViewController, you're assuming that the implementation of init for NSViewController searches for a nib with the same name as the view controller and uses it. However, this is undocumented API or at least I can't seem to find any documentation supporting that assumption. The link you posted on your comments doesn't cite any documentation either and even reiterates that this is undocumented and that Apple could change this implementation at any point.
I think to assure that your code works in future versions of the SDK (and since it is already creating undesired behavior), you should not rely on this assumption. To achieve the same outcome simply override init and initWithNibName:bundle: in such a way as explained by this post:
#implementation MyCustomViewController
// This is now the designated initializer
- (id)init
{
NSString *nibName = #"MyCustomViewController";
NSBundle *bundle = nil;
self = [super initWithNibName:nibName bundle:bundle];
if (self) {
...
}
return self;
}
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
// Disregard parameters - nib name is an implementation detail
return [self init];
}
I was trying to learn UIPageViewControllers and hit an Issue which I couldn't resolve.
This is what I tried to do:
Steps:
I simply created 2 view controllers and a page view controller in
StoryBoard.
Then I added some code to the File's Owner of PageViewController to
behave as a dataSource and delegate to itself.
When I ran, things worked well.
I added some buttons, and text fields to the second view controller.
I ran, worked well.
Now I added a text view to the second view controller and ran. When I tried to write something inside the text view, the page control jittered and moved to first view controller.
Has anyone experience this ever?
#interface AMPageViewController : UIPageViewController <UIPageViewControllerDataSource, UIPageViewControllerDelegate>
#end
The implementation:
#import "AMPageViewController.h"
#interface AMPageViewController ()
{
UIViewController *mainController;
UIViewController* socController;
}
#end
#implementation AMPageViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle: nil];
mainController = (UIViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: #"First"];
socController = (UIViewController*)[mainStoryboard instantiateViewControllerWithIdentifier: #"Second"];
[self setViewControllers:#[mainController]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
self.dataSource = self;
self.delegate = self;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
if (viewController == socController )
return mainController;
else return nil;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
if (viewController == mainController )
return socController;
else return nil;
}
- (NSInteger)presentationCountForPageViewController:(UIPageViewController *)pageViewController
{
return 2;
}
- (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)pageViewController
{
return 0;
}
#end
If you want to download and try the project
I've investigated a lot on this problem.
It seems a bug related to the internal (private) UIScrollView of the UIPageViewController.
If you search on StackOverflow you will find a lot of post with this problem and no solutions...
I seems that the UITextView (which is an UIScrollView and, AFAIR, has an internal UIWebView), sends some strange message to it's superviews chain, that makes the private UIScrollView of the UIPageViewController scrolling to the top-left corner.
I would have tried to block this message using method swizzling, but this is probably not ok for AppStore. So I tried other things.
The final solution is very simple: simply, embed your UITextView inside an UIScrollView!
This is a link to your project updated
If you do so, you'll solve the problem!
Try and let me know
EDIT:
How did I arrive to this solution:
An intuition.
A lot of debug and stack traces had make me think that the problem was related to a bug in the "nesting UIScrollView" system and some messages sent from the inner view to its superview.
UITextView inherits from UIScrollView and has inside an UIWebDocumentView (private) which is another UIScrollView. During the debug I saw a lot of messages (private methods) like "relayout superview" sent to the upper UIScrollView's. So, for some reason, the inner scroll view (UIWebDocumentView?) was sending a message/event to it's superview. This message/event (probably because of a bug) was not stopping to the external UITextView, and was forwarded to the UIScrollView handled by UIPageViewController.
Embedding the UITextView inside a simple UIView was not enough, because UIView forward the message to it's superview if it can't handle.
I thought: UIScrollView probably doesn't (otherwise it wouldn't simple to nest UIScrollViews), so I tried and it worked.
This is all a supposition because I stopped inspecting, I will have a more in-depth look this week.
Build target iOS-7.0.
The scrollview trick wasn't working for me. Tried to embed the textview in a scrollview through storyboard and code but no luck.
Simply delaying the call to the textview did it. Not very elegant, but its the only thing I've gotten to work so far.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.textView becomeFirstResponder];
});
}
Tested, working on my iPhone 5 and my ultra-slow iPhone4. Although its totally possible that whatever implementation detail enables the textview to become the responder could take longer than the set time. So keep in mind this isn't exactly bulletproof.
--EDIT--
Well... it's working on my iPhone 4 beater with a delay of 0.0000000000000001
you did not set before and after view controllers and also look in to first responder for socController
So, here's the issue. I'm relatively new to iOS programming, and I've taken on a giant project.
I'm working on a game with multiple levels which basically follow the same pattern, but have different sprite images and values, and I just decided to lay out all the levels in IB for speed's sake (not necessarily best practices, I know, but work with me). Each "level" has its own view controller, along the lines of "FireLevel1ViewController," "FireLevel2ViewController," etc. All of these view controllers inherit from a custom subclass of UIViewController I created called "GameController."
My problem is, when I open each level on my test device, GameController's viewDidLoad is getting called before the init or viewDidLoad methods of my subclass controllers, and so none of my level images/values are getting assigned to the superclass properties. Specifically, I have a pause menu that ought to be hidden at the outset of the level (I am doing setHidden in GameController's viewDidLoad), but since GameController's viewDidLoad runs before FireLevel1 has a chance to associate the correct IB property with PauseMenu, GameController just hides an empty view, and the actual PauseMenu never gets hidden.
I may have multiple problems going on here, but mostly I think I'm not really understanding correctly how to subclass a subclass of UIViewController and how to get the second subclass's properties/values/images to work in the first subclass's methods.
Thanks so much for any help! I hope that question made sense...
Code for GameController:
#implementation GameController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:pauseMenu];
[pauseMenu addSubview:helpMenu];
//Hides the pause and help menus until their respective buttons are pressed
[pauseMenu setHidden:YES];
[helpMenu setHidden:YES];
isPaused = NO;
}
Code for FireLevel1Controller:
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self)
{
theMainview = mainview;
theScene = scene;
theBG = bg;
theHudView = hud;
thePauseView = pauseMenu;
theHelpView = helpMenu;
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
firstTurret = [[StationaryEnemy alloc]init:turretImage1 baseView:base1];
secondTurret = [[StationaryEnemy alloc]init:turretImage2 baseView:base2];
NSLog(#"I'm in view did load");
}
Did you try using viewWillAppear? That method should be called after all the visible UI elements have been initialized.
Ahhhh I figured it out - I was accidentally negating my variables. I should've been doing mainview = theMainview; instead of theMainview = mainview - I was just assigning them all to zero. I also moved them all out of init into viewDidLoad, and moved [super viewDidLoad] underneath them, and now it works perfectly!
i started a single view template in Xcode 4.2(recently upgraded to Xcode 4.2 and ios5)
so now i have only one view controller.
I added a new class to the project which is a subclass of UIViewcontroller.
Now in the main controller class viewdidLoad method
- (void)viewDidLoad
{
// Override point for customization after application launch.
[super viewDidLoad];
[self presentQuizcontroller];
}
-(void) presentQuizcontroller
{
_QuizController = [[[Quiz alloc] initWithNibName:#"Quiz" bundle:nil] autorelease];
_QuizController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:_QuizController animated:YES]; // Do any additional setup after loading the view, typically from a nib.
}
the problem is in my Quiz class
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
the initWithNibName method does get called(i checked by using breakpoint) but it doesn't passes the condition of if(self) . and hence the view don't appears.
Any ideas?
Edit
After the first answer i tried this way too
- (void)viewDidLoad
{
// Override point for customization after application launch.
[super viewDidLoad];
}
-(void) presentQuizcontroller
{
_QuizController = [[[Quiz alloc] initWithNibName:#"Quiz" bundle:nil] autorelease];
_QuizController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:_QuizController animated:YES]; // Do any additional setup after loading the view, typically from a nib.
}
-(void) awakeFromNib
{
[self presentQuizcontroller];
}
same thing Quiz.m initwithnib name method does not passes the condition if(self).
I think you need to use awakefromnib.
Here is the Link for another StackOverflow post if you want to read more.
PresentModalViewController is depreciated, I think you should now use presentViewController instead of it.
Are you sure that "Quiz" is the name of your file? That string should be same as the name of your xib file, namely something like "QuizController" or "QuizViewController"
Make sure the xib file is properly connected to header/implementation files by checking:
Owner of the xib file should be set as the viewController
View on the xib file (the one above all if you have multiple views) should be connected to viewControllers view.
I've created a bare bones Master/Detail iPad application using the supplied template. It creates two view controllers (Master and Detail). I've created a view additional view controllers that get popped on top of the master view controller (pretty much drilling down tableviews until finally hitting a cell that populates the detail view. I've added the code below to load the master view controller popover to the specified dimensions (code below also shows when a selection is selected from the master view controller tableview):
#implementation MasterViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.vehicles = [[NSArray alloc] initWithObjects:#"Cars", #"Trucks", #"Boats", nil];
self.title = #"Vehicle Type";
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, [vehicles count] * 52.0);
}
return self;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (!self.studySessionViewController)
{
self.secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
}
[self.navigationController pushViewController:self.secondViewController animated:YES];
}
When the popover is selected when the application first loads, everything looks great. However, when a user clicks back to the MasterViewController, the size of the popover is the same size as the largest view controller that was pushed on the stack. I've searched around and I've added the following code in the Master view controller class:
- (void)viewWillAppear:(BOOL)animated
{
self.contentSizeForViewInPopover = CGSizeMake(320.0, [vehicles count] * 52.0);
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
self.contentSizeForViewInPopover = CGSizeMake(320.0, [vehicles count] * 52.0);
[super viewDidAppear:animated];
}
However, this has no effect. Also once the popover is closed (either by rotation or deselecting the popover on the UI), the popover size of the largest table view controller that was previously pushed on the stack is still retained and completely ignores the above dimensions. What am I missing?
I couldn't understand your whole problem but I think that the base of your problem is bad size of a Popover.
Usually, the problems with Popover size come of the fact that [UIViewController contentSizeForViewInPopover] can be set only before you create the UIPopoverController instance. It's just the default value for the popover size.
When you create the UIPopoverController instance with your controller as its contents, the contentSizeForViewInPopover is copied into [UIPopoverController popoverContentSize] and never read again. If you change it on the contents, Popover size is not updated.
The only way how you can change size of an already created UIPopoverController instance is using the property [UIPopoverController popoverContentSize] or method [UIPopoverController setPopoverContentSize:animated:].
You are trying to set the popover size in the MasterViewController itself. Here lies the issue. For self.contentSizeForViewInPopover = CGSizeMake(320.0, [vehicles count] * 52.0); to work you will have to use this in the DetailViewController instead, since the DetailViewController is the one that has a popover. Basic idea here is that you set the size in parent view controller and not the controller itself.
your going to have to setup the frames.
incomingCallViewController = [[IncomingCallViewController alloc]initWithNibName:#"IncomingCallViewController" bundle:nil];
[incomingCallViewController.view setFrame:CGRectMake(400, 200, 377, 243)];
[self.navigationController pushViewController:incomingCallViewController.view];
this of course is sample code from one of my projects, replace incomingCallViewController with your own class and declare it in the header