Can we assign UIView object to UIView object of XIB? - uiview

I have created UIView (say A) object through code and added all the sub views like UIButton, UILabel etc. through code. One other UIView (say B) I have created in XIB. now I am trying to assign A to B. When I Log the sub views of B after assigning, I can see all the sub views of A in B but nothing comes up in the view. I know my logic might be wrong but I didn't get it. Please let me know if its possible or not!
Here is my code:
- (UIView*)createButtonView:(NSInteger)viewTag avatarImageName:(NSString*)avatarImageName cardImageName:(NSString*)cardImageName ballImageName:(NSString*)ballImageName
{
buttonBGView = [[UIView alloc] initWithFrame:CGRectMake(15,86,290,290)];
buttonBGView.tag = viewTag;
UIImageView *avatarImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 206, 84, 84)];
avatarImageView.image = [UIImage imageNamed:avatarImageName];
[buttonBGView addSubview:avatarImageView];
UIButton *temoCardButton = [[UIButton alloc] initWithFrame:CGRectMake(58, 42, 188, 222)];
temoCardButton.tag = viewTag;
[temoCardButton setImage:[UIImage imageNamed:cardImageName] forState:UIControlStateNormal];
[temoCardButton addTarget:self action:#selector(cardButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[buttonBGView addSubview:temoCardButton];
UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(228, 0, 30, 30)];
scoreLabel.text = [cardPointsArray objectAtIndex:randomIndex];
scoreLabel.textAlignment = UITextAlignmentCenter;
[buttonBGView addSubview:scoreLabel];
buttonBGView.backgroundColor = [UIColor redColor];
return buttonBGView;
}
-(void)cardButtonTapped:(id)sender
{
NSLog(#"%#",[buttonBGView subviews]);
self.cardTappedView = buttonBGView;
NSLog(#"%#",[self.cardTappedView subviews]);
}
Thanks

Related

didTapInfoWindowOfMarker multiple buttons click event info

I am displaying some map points in google map. when user will click on that map point. I markerInfoWindow will be display. In this window, I am using two buttons. whenever user will click on that buttons. didTapInfoWindowOfMarker fires. but i amn't able to identify that which one has been clicked. may you please help me for that? i don't want to use any other approach.
Thanks
update:- This is infoWindow code.
-(void)prepareBubble
{
// UIView *bubble = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 80)];
self .frame = CGRectMake(0, 0, 260, 130);
self.backgroundColor = [UIColor clearColor];
// self.layer.borderColor = [UIColor grayColor].CGColor;
// self.layer.borderWidth = 1;
// self.layer.cornerRadius = 7;
UIImageView *bg = [[UIImageView alloc] initWithFrame:self.frame];
bg.image = [UIImage imageNamed:#"popup.png"];
[self addSubview:bg];
self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 5, 150, 40)];
self.titleLabel.backgroundColor = [UIColor clearColor];
[self.titleLabel setAdjustsFontSizeToFitWidth:TRUE];
self.titleLabel.numberOfLines = 0;
self.titleLabel.font = [UIFont fontWithName:FontNameArialBold size:14];
self.titleLabel.text = _title;
self.addressLabel= [[UILabel alloc] initWithFrame:CGRectMake(90, 40, 150, 35)];
self.addressLabel.backgroundColor = [UIColor clearColor];
[self.addressLabel setAdjustsFontSizeToFitWidth:TRUE];
self.addressLabel.numberOfLines = 0;
self.addressLabel.font = [UIFont fontWithName:FontNameArial size:12];
self.addressLabel.text = _address;
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 70, 70)];
[self addSubview:self.imageView];
self.imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_imageUrl]]];
if ( self.imageView.image == nil )
{
self.imageView.image = [UIImage imageNamed:DefaultImage100];
}
// [[[CustomNetwork alloc] init] setImage:_imageUrl onImageView:self.imageView withPlaceHolderImage:DefaultImage100];
[self addSubview:self.titleLabel];
[self addSubview:self.addressLabel];
if ( _ratings > 0 )
{
[self addSubview:[self addStar:_ratings]];
}
if( _hasTeeTimes ) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Book" forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
UIImage *buttonImage = [UIImage imageNamed:#"button_orange_large.png"];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(200, 75, 45, 25);
[self addSubview:button];
}
// return self;
}
here I am trying to make to click listener, first is on button and another is on tap in any part of this window like as textField etc. so my two new view will push
Change the buttons to properties.
Give the buttons tags.
Logically
determine by identifying the sender and see if it is > or < the other
button (which you need to keep in memory by making them properties or
instance variables).
load custom Xibs, and all of these will work with 0 lines of code.

How to Reset Password in Parse.com?

So when the user wants to recover their password they tap a button and a subview appears with aUITextField and a UIButton. After the user enters their e-mail I want the UIButton to send the email. I am missing the referral to the UITextField Here is my code:
- (IBAction)recoverPassword:(id)sender {
CGRect frame = CGRectMake(12, 51, 298, 497);
UIView *infoView = [[UIView alloc] initWithFrame:frame];
infoView.opaque = NO;
infoView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.8f];
infoView.userInteractionEnabled = YES;
infoView.tag = 01;
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(8, 200, 280, 30)];
textField.textAlignment = NSTextAlignmentLeft;
textField.textColor = [UIColor blackColor];
UIButton *getPassword = [[UIButton alloc] initWithFrame:CGRectMake(8, 265, 280, 36)];
getPassword.backgroundColor = [UIColor blackColor];
[getPassword addTarget:self
action:#selector(getUserPassword:)
forControlEvents:UIControlEventTouchUpInside];
[infoView addSubview:textField];
[infoView addSubview:getPassword];
[self.view addSubview:infoView];
}
- (void)getUserPassword:(UITapGestureRecognizer *)sendPassword {
[PFUser requestPasswordResetForEmailInBackground:**Missing Part**];
[[self.view viewWithTag:01] removeFromSuperview];
NSLog(#"Dissmissed Info SubView");
}
It looks like you are creating subviews (UITextField, UIButton, etc) only on the local scope (inside the method scope), thus they won't be accessible to other methods of the same class.
Try creating an ivar for the class you have these methods in, and assigning the values to the ivar, so the same variables will be accessible in your -getUserPassword: method.
P.S. Same approach, but applied to infoView, would eliminate the need to access it via viewWithTag, because you will be able to access it directly.

Not called button after adding in to container

I have container view, where I add some image and button.
Container view is property of UIView.
I'll add my Container View to ScrollView like this:
#property (nonatomic,strong) UIView *containerView;
..
containerView = [[UIView alloc] init];
for (int i=0;i<5;i++) {
UIImageView *whiteBack = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 235-height, 385-height-height)];
whiteBack.center = CGPointMake(_scrollView.frame.size.width * i+_scrollView.frame.size.width/2, self.view.bounds.size.height/2-131+height);
whiteBack.backgroundColor = [UIColor darkGrayColor];
whiteBack.layer.cornerRadius = 3;
whiteBack.layer.masksToBounds = YES;
[containerView addSubview:whiteBack];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 228-height, 340-height-height)];
img.center = CGPointMake(_scrollView.frame.size.width * i+_scrollView.frame.size.width/2, self.view.bounds.size.height/2-150+height);
img.image = [UIImage imageNamed:[NSString stringWithFormat:#"poster0%i.jpg",i+1]];
[containerView addSubview:img];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeCustom];
infoButton.frame = CGRectMake(233, 0, 25, 25);
infoButton.center = CGPointMake(_scrollView.frame.size.width * i+_scrollView.frame.size.width/2-100, 325-height);
[infoButton addTarget:self action:#selector(flipImage:) forControlEvents:UIControlEventTouchUpInside];
infoButton.tag = i;
infoButton.backgroundColor = [UIColor clearColor];
[infoButton setImage:[UIImage imageNamed:#"upload-50"] forState:UIControlStateNormal];
[infoButton setTintColor:[UIColor whiteColor]];
[containerView addSubview:infoButton];
}
[self.scrollView addSubview:containerView];
///
- (void) flipImage :(id) sender {
NSLog(#"infoButton pressed:%i",[sender tag]);
}
but when I pressed button nothing happens!
Your container view has a zero frame (because you just did [[UIView alloc] init];). This means that it never receives any touches because you can never hit it. This goes for the subviews too.
The reason you see the subviews is that views, by default, do not clip drawing to their bounds.
To fix, set the frame of your container view to an appropriate size for the subviews you're adding.

Set custom titleLabel properties for multiple UIButtons

I've got a custom button like:
UIButton *catBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
catBtn1.frame = CGRectMake(0, 344, 427, 67);
catBtn1.titleLabel.font = categoryFont;
catBtn1.titleLabel.textColor = [UIColor whiteColor];
catBtn1.titleLabel.frame = CGRectMake(52, 21, 150, 15);
catBtn1.titleLabel.textAlignment = UITextAlignmentLeft;
Is there a quick way to set all these titleLabel properties for all my buttons without doing this for each button? I tried assigning an instance of UILabel to catBtn1.titleLabel but that didn't work...
You can create method for button like this...
-(UIButton *) customBtn:(NSString *)btntxt:(CGFloat)btnx:(CGFloat)btny:(CGFloat)btnwidth
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//[btn setTitle:btntxt forState:UIControlStateNormal];
btn.titleLabel.text = btntxt;
btn.titleLabel.font = categoryFont;
btn.titleLabel.textColor = [UIColor whiteColor];
btn.titleLabel.frame = CGRectMake(52, 21, 150, 15);
btn.titleLabel.textAlignment = UITextAlignmentLeft;
[btn setFrame:CGRectMake(btnx, btny, btnwidth, 40)];
return btn;
}
and you can call it like this any number of time when you need it
[YOURVIEW_OBJECT addSubview:[self customBtn:#"Samanmalliset" : 165:20 :115]];
You can create method that sets these properties and call it for each of your buttons.

How can I know which view I am clicking? And why the button no reaction?

I am designing a viewcontroller which have several button bar, each bar canbe clicked and show a content view.like:
http://i.stack.imgur.com/m5V4Q.png
When I click the buttonbar, it's frame will expand bigger, and you can see the content in it(which is a button).
First, a bar button(320*30 size) and a contentView is a set, they are subviews of listview. And several listview makes the whole view.
Second, when I click it, the list view will expand from 320*30 to 320*180, the contentview in it will expand from 300*0 to 300 * 130(20 pixel of margin). and I set the clipsToBounds of contentview to Yes to make sure the content in contentview won't show when the frame height is 0.
Now,the click can show the contentview exactly as I want. But here is a problem: I can't click the button in it, and I tried to set their userInteractionEnabled to yes . And even I set the contentview as user-defined,which I set the view's userInteractionEnabled to yes and overwrite the touchbegin function to show a alertView. Those test are failed, no reaction found. And I checked and sure that there shouldn't be any view covered it.
What's the reason might be?
The code of setup the list view is:
NSArray *array = [titleArray objectAtIndex:i];
NSString *ShenSuoTitle = [array objectAtIndex:0];
NSString *ShenSuoContent = [array objectAtIndex:1];
UIView *listView = [[UIView alloc] initWithFrame:CGRectMake(0, totalHeight, 320, ShenSuoViewHeight)];
[listView setBackgroundColor:[UIColor blackColor]];
[listView setTag:[[NSString stringWithFormat:#"%d",(i+1)] intValue]];
[self.scrollView addSubview:listView];
totalHeight = totalHeight + 1 + ShenSuoViewHeight;
UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[titleView setBackgroundColor:[UIColor whiteColor]];
[titleView setTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
[listView addSubview:titleView];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setTag:[[NSString stringWithFormat:#"%d2",i+1] intValue]];
[btn setFrame:CGRectMake(0, 0, 320, ShenSuoViewHeight)];
[btn addTarget:self action:#selector(reSetFrame:) forControlEvents:UIControlEventTouchUpInside];
[titleView addSubview:btn];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(5, 12, 30, 30)];
img.image = [UIImage imageNamed:#"list_ico.png"];
[img setTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
[titleView addSubview:img];
NSLog(#"img:%f,%f",img.frame.origin.y,img.frame.size.height);
UILabel *labTitle = [[UILabel alloc] initWithFrame:CGRectMake(35, 15, 100, 25)];
labTitle.textColor = [UIColor blackColor];
labTitle.backgroundColor = [UIColor clearColor];
labTitle.font = [UIFont boldSystemFontOfSize:15];
labTitle.text = ShenSuoTitle;
[titleView addSubview:labTitle];
NSLog(#"labTitle:%f,%f",labTitle.frame.origin.y,labTitle.frame.size.height);
//add a label for selected
UILabel *selectedLabel = [[UILabel alloc] initWithFrame:CGRectMake(214, 14, 86, 21)];
selectedLabel.textColor = [UIColor grayColor];
//selectedLabel.alpha = 0.75;
selectedLabel.backgroundColor = [UIColor clearColor];
selectedLabel.text = #"All";
selectedLabel.font = [UIFont boldSystemFontOfSize:15];
[selectedLabel setTag:[[NSString stringWithFormat:#"%d5",i+1] intValue]];
[titleView addSubview:selectedLabel];
NSLog(#"selectedLabel:%f,%f",selectedLabel.frame.origin.y,selectedLabel.frame.size.height);
UIView *content = [[UIView alloc] initWithFrame:CGRectMake(10, 10 + ShenSuoViewHeight, 300, 0)];
content.backgroundColor = [UIColor grayColor];
UILabel *testLa = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
testLa.text = #"Label";
UIButton *testBut = [UIButton buttonWithType:UIButtonTypeRoundedRect];
testBut.frame = CGRectMake(0, 40, 100, 30);
testBut.titleLabel.textColor = [UIColor greenColor];
testBut.titleLabel.text = #"Button";
content.tag = [[NSString stringWithFormat:#"%d4",i+1] intValue];
[content addSubview:testLa];
[content addSubview:testBut];
content.clipsToBounds = YES;
[testLa release];
[listView addSubview:content];
[content release];
[labTitle release];
[img release];
[titleView release];
[listView release];
the code handle the click to make the list view expands is:
UIView *titleView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d1",i+1] intValue]];
titleView.backgroundColor = [UIColor colorWithRed:0.92 green:0.92 blue:0.92 alpha:0.9];
UIView *listView = (UIView *)[self.view viewWithTag:[[NSString stringWithFormat:#"%d",i+1] intValue]];
listView.frame = CGRectMake(0, totalHeight, 320, ShenSuoViewHeight);
totalHeight = totalHeight + 1 + 20 + 180 + ShenSuoViewHeight;
UIView *content = [self.view viewWithTag:[[NSString stringWithFormat:#"%d4",i+1] intValue]];
content.frame = CGRectMake(10, 10 + ShenSuoViewHeight, 300, 180);
UIImageView *img = (UIImageView*)[self.view viewWithTag:[[NSString stringWithFormat:#"%d3",i+1] intValue]];
img.image = [UIImage imageNamed:#"list_ico_d.png"];
I'm not sure I follow your situation exactly, and some code would be nice. To answer your question though, the reason is most likely that the touch is being intercepted elsewhere. It may also be that you just forgot to connect an action method to the button. Try using breakpoints or NSLogs to see what methods are being triggered and which aren't.
Thanks a lot. It is due to my fault. The code of expanding the bar which setting the listview's frame is wrong, it should add in the expanded height. This leads the visual is ok but the clicking not be handled.
So this problem closed.

Resources