I am creating a custom UIView called DropDown that contains a button. In the .h file I initialize the button and in the constructor of the UIView, I add it to the frame and set the title. Here is the constructor:
-(id) initWithFrame:(CGRect)frame title1:(NSString*)text1{
self = [super initWithFrame:frame];
if (self){
_button1 = [UIButton buttonWithType:UIButtonTypeCustom];
[_button1 setTitle:text1 forState:UIControlStateNormal];
_button1.frame = CGRectMake(10, 10, 50, 20);
[self addSubview:_button1];
}
return self;
}
In my ViewController viewDidLoad I am instantiating a DropDown object as follows:
_dropDown = [[DropDown alloc] initWithFrame:_dropDown.frame title1:#"Title"];
The _dropDown object is an IBOutlet and I hook it up on the view controller. When I run the application, no button is shown and I am unsure why. Any help would be much appreciated.
This statement
_dropDown = [[DropDown alloc] initWithFrame:_dropDown.frame title1:#"Title"];
references _dropDown.frame which is nil at this point because _dropDown hasn't been assigned yet. I would suggest creating the frame explicitly more like this:
_dropDown = [[DropDown alloc] initWithFrame:CGRectMake(10, 10, 50, 20) title1:#"Title"];
(you'll need to figure out the sizing for CGRectMake)
Related
In a game I am making, I made a class file called bomb. In the file, I have a method called displayBomb:
- (void) displayBomb
{
bombImage = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
}
The method is supposed to make an image display on the screen. In my ViewController.m file in ViewDidLoad, I have:
bomb *bomb1 = [[bomb alloc] init];
[bomb1 setValue:[UIImage imageNamed:#"image.png"] forKey:#"bombImage"];
[bomb1 displayBomb];
It doesn't display anything, though. I think the problem is that I need something like [self.view addSubview:bombImage]. When I put that in the ViewController.m file, it says Use of undeclared identifier 'bombImage'. When I put that in bomb.m, it says property "view" not found on object of type bomb *. I figure I need something like [ViewController.view addSubview:bombImage], but that returns property "view" not found on object of type ViewController.
You can't call a method on an object you don't have a reference to. Your view controller needs to know about bombImage or your bomb needs to know about your view controller. You could make bombImage a property of bomb and then in your view controller do this:
[self.view addSubview:bomb1.bombImage];
Edit: The typical way I'd assign an image on bombImage would be the following:
bombImage = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
[bombImage setImage:[UIImage imageNamed:#"image.png"]];
add view parameter to your method:
- (void) displayBombOnView:(UIview *)view
{
bombImage = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
[view addSubview:bombImage];
}
in viewDidLoad call method:
[bomb1 displayBombOnView:self.view];//or which view is on superview
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C function calling objective C functions
I've done the following code in ViewController.m
-(void) callIncomingCreateButton
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
button.frame = CGRectMake(100, 170, 100, 30);
//set the button's title
[button setTitle:#"Click Me!" forState:UIControlStateNormal];
//add the button to the view
[self.view addSubview:button];
}
- (IBAction)DemoCall:(id)sender {
callIncoming(1, "a");
}
int callIncoming(int a, char* b)
{
ViewController * tempObj = [[ViewController alloc] init];
[tempObj callIncomingCreateButton];
return a;
}
But still the UIButton is not getting displayed, what am I missing here.
Edit : this is for iOS
and yes, the function does get called. I put a breakpoint and stepped it through.
ViewController * tempObj = [[ViewController alloc] init];
[tempObj callIncomingClass];
It's hard to tell from your example, but assuming that -callIncomingClass is actually part of the definition of ViewController and callIncoming() is called from somewhere else, a likely problem is that tempObj is not the current view controller. You'd need to push tempObj onto the navigation stack, present it modally, or otherwise make it the active view controller in order for its view to be loaded. If its view isn't loaded, then there's no view to add the button to, and even if it has a view, that view won't be in the window.
I am currently building a Profile Object that can return a view with all the information of the user. I want to be able to reuse the code in many different places so I am trying to build it in an NSObject but when I add it as a subview of my view, I cannot click the buttons. The buttons are created with a rectangle and then have a UILabel put on top of them. If I copy the cody where I am trying to put it, it works. I also tried creating a delegate but that did nothing either.
EDIT
UIView *profile = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 175.0)];
UIView *votesView = [[UIView alloc] initWithFrame:CGRectMake(60.0, 125.0, 60.0, 50.0)];
UILabel *votesLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 0.0, 50.0, 40.0)];
votesView.backgroundColor = [UIColor lightGrayColor];
votesLabel.text = votes;
votesLabel.textAlignment = UITextAlignmentCenter;
votesLabel.backgroundColor = [UIColor clearColor];
votesLabel.tag = 2;
[votesView addSubview:votesLabel];
[profile addSubview:votesView];
Why are you subclassing an NSObject, you can subclass an UIView and implement all your views that are to be included,
Also, I am not able to see how you are adding a button, I can see just labels in your code.
But would suggest that you need to mention action and target, where for target you can add your class where it is called
[btn addTarget:<class where you will add UIView returned by your class> action:<SEL in your class where you will add UIView> forControlEvents:<UIControlEvents>]
the problem I've met today is with my subclass of UIPageControl. When I initialize it, the frame (specifically the origin) and image of dots stays default, which is the problem, since I want it to change right after initialization. However, when I move with scrollView (as in "touch and move") after initialization, they (the dots) somehow jump to the right position with correct images.
What could be the problem?
Code:
CustomPageControl.m
- (id) initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
activeImage = [UIImage imageNamed:#"doton.png"];
inactiveImage = [UIImage imageNamed:#"dotoff.png"];
return self;
}
- (void) updateDots
{
for (int i = 0; i < [self.subviews count]; i++)
{
UIImageView *dot = [self.subviews objectAtIndex:i];
if (i == self.currentPage) dot.image = activeImage;
else dot.image = inactiveImage;
[dot setFrame:CGRectMake(i * 13.5, 1.5, 17, 17)];
}
}
- (void)setCurrentPage:(NSInteger)currentPage
{
[super setCurrentPage:currentPage];
[self updateDots];
}
#end
ChoosingView.m - init part
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 160, 300)];
[scrollView setBackgroundColor:[UIColor clearColor]];
[scrollView setDelaysContentTouches:NO];
[scrollView setCanCancelContentTouches:YES];
[scrollView setClipsToBounds:NO];
[scrollView setScrollEnabled:YES];
[scrollView setPagingEnabled:YES];
[scrollView setShowsHorizontalScrollIndicator:NO];
[scrollView setShowsVerticalScrollIndicator:NO];
pageControl = [[CustomPageControl alloc] initWithFrame:CGRectMake(200, 300, 80, 20)];
[pageControl setBackgroundColor:[UIColor clearColor]];
pageControl.numberOfPages = 6;
[pageControl setCurrentPage:0];
the last line is when I would expect the UIPageControl to refresh, however that does not happen.
Does this happen with the standard UIPageControl implementation?
Your problem states that your objects subViews (eg the UIImageViews) rects/size are not initialising to your desired size/position.
I implemented this code in my project with a nib rather than programmatically and I needed to call -(void)updateDots to set it as its initial condition was the standard dots..
I dont see how the UIScrollView has any bearing impact on this unless somehow its linked to your -(void)updateDots function (E.g. your setting the currentIndex of your custom page control). You state, "However, when I move with scrollView (as in "touch and move") after initialization, they (the dots) somehow jump to the right position with correct images."
Because they "jump to the right position with correct images" it means that your -(void)updateDots function must be getting called. I dont see any other explanation.
Also your iteration loop assumes that all the UIViews in your .subViews array are UIImageViews, although fairly safe to assume this, I would check to see if the UIView is a UIImageView with reflection.
So I created a custom UINavigationItem category to be able to make a custom titleview for my navbar, but everytime I push/pop a view, it simply adds the new title without getting rid of the old one causing the title to just be a jumble of letters. Here's the relevant code:
#implementation UINavigationItem (CustomNavigationItem)
-(UIView *)titleView
{
[self setTitleView:nil];
UILabel *newTitleView = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 220, 32)];
newTitleView.center = CGPointMake(160, 22);
newTitleView.backgroundColor = [UIColor clearColor];
newTitleView.textColor = [UIColor whiteColor];
newTitleView.textAlignment = UITextAlignmentCenter;
newTitleView.text = self.title;
newTitleView.textAlignment = UITextAlignmentCenter;
return newTitleView;
}
#end
You have to remove the old uilabel from its superview, by setting to nil it doesn't do that. That's why you are messing the letters on screen. I also do not think you are getting a recursion, because you are caling the setter, but I maybe wrong.
A quick thing you could is to assign a tag to your newest created view.
[[self.view viewWithTag:YourCustomEnumTag] removeFromSuperView];
// create your view....
textView.tag=YourEnumCustomTag;