I have these two pieces of code. The first one works perfectly:
UIView *tmp = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 60.0f, 296.0f, 44.0f)];
[self.dynamicView addSubview:tmp];
[tmp release];
The second one is pretty much the same, but the view doesn't show up.
CommentBox *commentBox = [[CommentBox alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 296.0f, 44.0f)];
[self.dynamicView addSubview:commentBox];
[commentBox release]; // Why does this remove the view?
If I remove the [commentBox release] the view surprisingly appears. But I don't see a different between these two code snippets.
The init for the CommentBox looks like this:
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
// Load the nib:
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"CommentBox" owner:self options:nil];
self = [nibObjects objectAtIndex:0];
}
return self;
}
After thinking about Graham's answer I came up with the following solution:
I drag a new UIView (-> lets call it sub UIView) in Interface Builder in my main UIView
I give this sub UIView the correct size (because I cannot resize the main UIView, which is always 320x460)
I drag all my other elements in this sub UIView (so that all elements are attached to my sub UIView)
I give my sub UIView a tag number (Interface Builder -> View Attributes), e.g. "300"
In the code I do now the following within my -initWithFrame:
NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:#"CommentBox" owner:self options:nil];
UIView *subView = [[nibObjects objectAtIndex:0] viewWithTag:300];
[self addSubview:subView];
Hope that helps.
Update:
I just had another idea of doing it. Instead of the tag numbers you can also create a IBOutlet UIView *viewHolder in the CommentBox class and set the outlet in IB. Then in the initWithFrame: I do the following:
[[NSBundle mainBundle] loadNibNamed:#"CommentBox" owner:self options:nil];
[self addSubview:self.viewHolder];
You're doing weird things in -initWithFrame:. I'm not 100% sure that it's causing the problem you report, but I'm pretty sure it's:
wrong; and
causing a memory leak.
I don't think replacing a view object with something dearchived from a nib in its -init… methods is a good idea. Either load the nib from a controller class, or have your initialiser load the object's subviews from the nib without replacing self.
Related
I have a custom made uiview class, and i have a dealloc with other releasing call within it.
In this custom uiview, I have 3 outlet of controls : buttons, textview etc. When the custom uiview deallocated, I received this error: Error for object 0x88782c0: pointer being freed was not allocated.
Inside the code, I have not release any of these outlet at all. When I removed those 3 outlet of release call, the error was gone. Do I need to deallocate those outlets.
[[[[NSBundle mainBundle] loadNibNamed:#"ParanoidView" owner:self options:nil] objectAtIndex:0] autorelease];
[self.view setFrame:CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height)];
[self addSubview:self.view];
The above code is add within the initWithFrame call to load the nib file.
There are many things wrong with your code.
[[[[NSBundle mainBundle] loadNibNamed:#"ParanoidView" owner:self options:nil] objectAtIndex:0] autorelease];
You cannot release or autorelease something you didn't retain. Period.
learning much from you guys, and after a few hours I finally managed to load a xib into a GRect for a search xib that I built.
Basically, when you tap a cell in main.xib, it loads search.xib in a Grect so that I can filter down a list, click o nthe value, and place back into the cell in main.xib.
However I still have the cells from main.xib that overlay (tvcells) that overlay my search.xib.
Also it seems that none of the code is initializing for search.xib, I put an NSlog message in viewdidload in the search.xib, and nothing shows up in the console, letting me know that none of that code was executed.
Any idea why?
Thanks!
CGRect container = [[UIScreen mainScreen]bounds];
container.origin.x = 0;
container.origin.y = 0;
NSArray *views = [[NSBundle mainBundle] loadNibNamed: #"ingredientSearchViewController" owner: self options: nil];
UIView *referencedView = (UIView *)[views objectAtIndex:0];
referencedView.frame = container;
[self.subView addSubview:referencedView];
NSLog(#"loaded subview but obviously not in subview xib");
Additional code that attempts to do this as a controller
if (self != nil){
[self.view removeFromSuperview];
}
if(self == nil){
ingredientSearchViewController *vc = [[[ingredientSearchViewController alloc]initWithNibName:#"ingredientSearchViewController" bundle:nil] autorelease];
vc.view.frame = [UIScreen mainScreen].applicationFrame;
vc.delegate = self;
[self.subView addSubview:vc.view];
}
viewDidLoad is a method in NSViewController.
And you are creating a View from Nib, therefore you should use the equivalent method awakeFromNib for NSView.
I have a code that load the custom view from a nib file but there is a problem with the variable of that custom view.
- (id)initWithFrame:(CGRect)frame
{ self = [super initWithFrame:frame];
if (self) {
// Initialization code
HomeMainView* views = (HomeMainView*)[[[NSBundle mainBundle] loadNibNamed:#"HomeMainView" owner:self options:nil] objectAtIndex:0];
[self release];
NSArray* permission = [NSArray arrayWithObjects:#"user_photos",#"publish_stream", nil];
FBLoginView* fbLogin = [[[FBLoginView alloc] initWithPublishPermissions:permission defaultAudience:FBSessionDefaultAudienceFriends] autorelease];
[views.FBLogin addSubview:fbLogin];
[views.homeButton setTitle:#"asdf" forState:UIControlStateNormal];
self = views;
}
return self;
}
I am trying to add the fbLogin to the views object generated by the Nib file. The problem is the views.FBLogin generate the errors saying unrecognized selectors.
Does anyone know what is wrong here?
I had the same problem but it was because I needed to add this to my app delegate didFinishLaunchingWithOptions :
[FBLoginView class]
Without this the UIView was a UIView not an FBLoginView, hence the unrecognized selector.
Make sure that you have set the custom class in interface builder for the view. Select the view and press cmd+option+3 and set the custom class.
I have been looking all over fo information on how to do this but have found nothing that I understand.
Basically I want to be able (from ViewController.xib) to load another XIB file on top of that view in a subview. This new subview should be manipulatable, so it can be moved around with multitouch gestures and/or swipe gestures.
I was trying to add a "view" object from ViewController, then load the other XIB into that subview object.
Hopefully someone can help. Thanks!
You should be able to accomplish what you want by making an instance of the other class in your ViewController, and then adding it as a subview to the current view controller.
MyOtherViewController *movc = [[MyOtherViewController alloc] init];
[self.view addSubview:movc.view];
As for handling the gestures, you could either handle them in the MyOtherViewController class, or make a container view and handle them it in your ViewController. Don't forget that they are subviews, and that any movement should be relative to their superviews.
The above code is almost right but except
UIView *newView = [[NSBundle mainBundle] loadNibNamed:#"MyNewXib" owner:self options:nil];
it should be
UIView *newView = [[[NSBundle mainBundle] loadNibNamed:#"MyNewXib" owner:self options:nil] objectAtIndex:0];
and in a view controller simply you can add
[self.view addSubview:newView];
You can define the subview in a xib by creating a xib (select File New... "user interface", "view").
You can load that view like this:
UIView *newView = [[NSBundle mainBundle] loadNibNamed:#"MyNewXib" owner:self options:nil];
That new view can now be treated like any other subview:
// assign it to a property
#property (strong, non atomic) UIView *viewFromXib;
#sythesize viewFromXib=_viewFromXib;
self.viewFromXib = newView;
// add it to your hierarchy
self.viewFromXib.frame = CGRectMake( /* this will start out 0,0 the size from the xib */ );
[self.view addSubview:self.viewFromXib];
// add gesture recognizer
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)];
[self.viewFromXib addGestureRecognizer:tapGR];
... and so on
I am new in iPhone development.
I have a UIViewController in storyboard that includes a scrollView.
I want to put two UIImageViews and some UILabels on it.
the idea that I am thinking is : create a .xib file. Put a UIView on it, an then set my UIImageview and labels on this UIView.
Finally add my UIView which is a .xib file to my UIScrollView.
Is it a good idea? if not what is your suggestion.
until now I could not make it works. this is my code:
OfferUIView *newPageView = [[OfferUIView alloc] init]; // UIView that I created as .xib
self.newPageView.contentMode = UIViewContentModeScaleAspectFill;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
Can you help me?
I know it sounds stupid, but have you tried removing the self from self.scrollView and self.pageViews ?
How did you created your .xib file? it should be sub class of UIView. Hope you didn't do the mistake of creating it as a sub class of UIViewController.
I found the answer:
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"OfferView" owner:self options:nil];
UIView *newPageView = [views lastObject];