I have an xib file includes a UIView and a UIScrollView.
Inside my UIView, I have another UIView (the while screen in the middle), and I want to add my scrollView to it programmatically.
The reason is one time I want to add a scrollView there and another time I want to add a mapView.
What I did is like this:
CGRect frame = self.view.bounds;
self.scrollView = [[UIScrollView alloc] initWithFrame: frame];
CGSize pagesScrollViewSize = self.view.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
I also need to set the delegate for the UIScrollView.
I could not make it work. this is my error:
-[UIScrollView setPageId:]: unrecognized selector sent to instance 0x7dd84e0
2012-10-06 00:56:17.373 testN[3525:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIScrollView setPageId:]: unrecognized selector sent to instance 0x7dd84e0'
I also don't know where should I connect the scrollView delegate. can you help me?
The error you're getting isn't in the code that you have pasted, it's because you probably have some code such as self.scrollView.pageId = ... somewhere else
Regardless of that, to help you achieve the effect you want you should probably add IBOutlet references for the MKMapView, UIScrollView and white UIView in the middle of the screen to your view controller.
You can also set the delegate of the scrollView in your xib file by right clicking on it and dragging the circle next to delegate (under Outlets) to the Files Owner (assuming the files owner is a UIViewController that implements UIScrollViewDelegate)
Then in viewDidLoad you'd have something like this
- (void)viewDidLoad {
[super viewDidLoad];
self.scrollView.contentSize = self.scrollView.frame.size;
self.scrollView.frame = self.whiteCenterView.bounds;
[self.whiteCenterView addSubview:self.scrollView];
}
You shouldn't alloc/init a scroll view manually in code as you already have it there in the xib file and it will already be automatically created by the time viewDidLoad is called in your controller.
I can do that successfully by adding a UIScrollView in the interface builder and then set the delegate and file owner from there.
I just should note that since I want to add the UIView to a ViewController in storyboard this code will not work:
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"OfferView" owner:self options:nil];
UIView *newPageView = [views lastObject];
I should use:
UIView *newPageView = [views objectAtIndex:0];
Related
It's a little bit to explain why I'd like to implement this design.
But the question I want to ask is : If I have a view and it's controller is ControllerA, and now I want to add a subview in my that view suppose View1. And that subview View1 contains a button, which I set the IBAction point to the ControllerA.
But I found that this View1 button cannot change some properties in original view's object like UIImageview.hidden.
Can't a button in subview alter things inside superview ? Or I need to set up other things to finish this task.
i guess you were try to add a button inside the UIView which is also a subview of View(UIViewController).
as you told whenever you try to access your Button property then you could not do same.
this is happening because whenevre we create anything through the XIb if we want access that UIControl or whatever then we have make Reference of that in Our Source Code.so you'll have hook up the UIButton with reference from the Xcode.
As i am doing in below Image.
I hope i got your Point. it'll be helpful to you
It's hard to understand exactly what you're saying but it sounds like you're trying to access a subview of a UIView instance outside of that UIView. If it is not a UIView subclass where you have an ivar/property reference to that subview, you can try giving it a tag and accessing it that way.
example:
UIView *topLevelView = [[UIView alloc] init];
UIView *viewA = [[UIView alloc] init];
UIView *otherView = [[UIView alloc] init];
otherView.tag = 5;
[topLevelView addSubview:viewA];
[topLevelView addSubview:otherView];
UIView *viewASubview = [[UIView alloc] init];
[viewA addSubview:viewASubview];
UIView *referenceToOtherView = [viewASubview.superview.superview viewWithTag:5];
UIView *anotherReferenceToOtherView = [topLevelView viewWithTag:5];
I'm looking for a sample application that uses Apple's new GLKView object whith other UIObjects like UIButton in the same window. I was searching that from the internet and the more like post I found wasthat and is not answered:
In the application I'm creating I need to use the GLKView with 2 UIButton in the same view. Depending of what Button I press I change the parameters of the GLKView. How the ViewController should handle the GLKView? Should I have a GLKViewController separate of the actual view controller to controll only the GLKView?
I tried to make my view controller inherit GLKViewController.
#interface ViewController : GLKViewController <GLKViewControllerDelegate, GLKViewDelegate>
And set up the GLKView named OpenGLView in the viewDidLoadFunction:
- (void)viewDidLoad
{
[super viewDidLoad];
EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
openGlView.delegate = self;
openGlView.context = aContext;
openGlView.drawableColorFormat = GLKViewDrawableColorFormatRGBA8888;
openGlView.drawableDepthFormat = GLKViewDrawableDepthFormat16;
openGlView.drawableMultisample = GLKViewDrawableMultisample4X;
self.delegate = self;
self.preferredFramesPerSecond = 30;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
The build goes well but when I run the application it crash saying:
'NSInternalInconsistencyException', reason: '-[GLKViewController loadView] loaded the "2-view-5" nib but didn't get a GLKView.'
Can someone help me? I was searching an example for this but I doesn't find it
Jordi P
The error message you're getting means that the GLKViewController subclass, your viewController, cannot find the expected outlet to a GLKView. In IB the view property of your viewController MUST be connected to a GLKView (or subclass) to work correctly.
Go to the Storyboard, select the view inside the GLKViewController, and set it as GLKView class.
I have a ViewController that responds to some touchEvents (touchesBegan, touchesMoved, etc...).
I've found that when I show this controller using presentModalViewController: it works just fine, but I'm trying to add it's View as a subview of another ParentViewController like so:
- (void)viewDidLoad
{
[super viewDidLoad];
//Add SubController
controller = [[SubViewController alloc] initWithNibName:#"SubViewController" bundle:nil];
controller.view.frame = CGRectMake(0, 30, 300, 130);
[view addSubview:controller.view];
[controller release];
}
When I do this, it gets added the parent view but it no longer responds to touch events. Is there a way to fix this?
Also, is there a better way to go about this? I know I probably could have used a View subclass for the child view, but it's supposed to use a Nib and I wasn't sure how to handle that without using a ViewController.
You're correct you should use a UIView subclass.
The easiest way to load it from a nib is to include the subview in your nib.
Just drop a UIView into the view connected to the original view controller.
Then with the view inside selected go to the identity inspector. It's the one that looks like a little ID card.
The very first field is called Custom Class.
Type the name of your UIView subclass here.
If you need a reference to this just create an IBOutlet in your original view controller and hook it up. That way you can set hidden = YES until you need it.
In your UIView subclass you might want to override
- (void)awakeFromNib
This will get called when the nib first unpacks.
for setting up any gesture recognizers, etc.
To load a nib directly into a view :
// Get the views created inside this xib
NSArray *views = [NSBundle mainBundle] loadNibNamed:#"myViewNib" owner:nil];
// There's probably only one in there, lets get it
UIView *myView = [views objectAtIndex:0];
// Do stuff . . .
[[self view] addSubview:myView];
You could try to call becomeFirstResponder in your subview and see whether it receives touchesBegan... It is probably so, but it will also possibly make the superview not receive touchesBegan if you require it...
I have a nib file I'm trying to instantiate in code. My UIViewController's main view is also loaded from a nib file.
Here's my UIViewController's viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:#"MyCustomView" bundle:nil];
NSArray *nibViews = [nib instantiateWithOwner:self options:nil];
MyCustomView *myView = [nibViews objectAtIndex:0];
myView.frame = CGRectMake(100.0f, 100.0f, 91.0f, 91.0f);
[self.view addSubview:myView];
}
This creates some sort of endless loop. If I comment out [self.view addSubview:myView], myView appears, but everything currently on the screen disappears. I didn't think instantiateWithOwner added the view to the screen. If it does, how do I get access to it?
Thanks for your help.
The instantiateWithOwner method reassigns the properties of your view controller (set from the nib the controller was created from) to ones from the new nib. Those properties likely include the view property, so that method, within it, contains a call to setView:, and sets the view controller's view to the new nib's view. Afterwards, you're trying to add a view as a subview to itself, and that, naturally, causes problems.
You want to create your own property, for instance, secondaryView, set the nib's view to that, and add it as a subview. You don't want to reassign your view controller's view.
I am initializing a new UIViewCOntroller object.
then attempting to set its view's position of stage but I am having some trouble.
here is the code I am using
Note: this code is placed in the application main UIViewController's viewDidLoad method
UIViewController * cont = [[UIViewController alloc] init];
cont.view.backgroundColor = [UIColor redColor];
CGRect rect = CGRectMake(100, 0, 320, 480);
cont.view.frame = rect;
this code is still positioning the subview at (0,0) instead of (100,0)
However, if I introduce a decimal, such as using 320.01 (for the width value) or 480.01 (for the height value). The view would be positioned correctly.
It seems that if I use a size with an exact width:320.0 height: 480.0,
the origin will always be set to (0,0) !!!
This is a bit strange. I was hoping that someone could explain why this is happening, and possibly how it may be resolved.
Cheers ....
NSLog the value of cont.view and I think you will find it to be nil, which explains why nothing's happening. This is not the normal way to create a UIViewController -- it's not wrong to create one programmatically, but 99.99% of the time UIViewController subclasses are created with the main UIView in a .xib file. A freshly created UIViewController object has a nil "view" member, so you've got to initialize it somehow, either by loading a .xib:
MyViewController *vc = [[[MyViewController alloc] initWithNibName#"MyViewController" bundle:nil] autorelease];
or manually creating the view:
MyViewController *vc = [[[MyViewController alloc] init] autorelease];
UIView *theView = [[[UIView alloc] initWithFrame:viewframe] autorelease];
vc.view = theView;
Then you can move the view's frame to your heart's content, but moving the base view of a view controller is usually not what you want to do, you want to create sub-views and move those around.
[[UIViewController alloc]init] is wrong. The designated initializer for UIViewController is initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle. Even that does not necessarily initialize the view outlet to an actual UIView immediately. You need to subclass UIViewController and perform your customisations in the viewDidLoad method of that subclass.
In the interim is likely that view is nil so you can try setting whatever properties of it you like without anything ever happening.
I think you should be able to use
-(void) loadView {
[super loadView];
//create your views programmatically here
}
in order to create your viewController programmatically and avoid the IB. Normally the IB calls this method for you when your 'view' property is nil, however if you're avoid the IB make sure to include the above method so your view property is not nil.