Multiple UITextfield Create Dynamically in iphone sdk - iphone

I have to create a multiple textfield dynamically on scrollview in ipad app. textfield created successfully and i use popup view on clicked textfield but when i clicked on textfield i dont get the Tag of Textfield. i get tag of last textfield so i cant set the text on the current textfield.
Following code i used...
int j = 430;
int k = 413;
int RB = 423;
for (int i=0; i<[appDelegate.questions count]; i++) {
imgTxtQue = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"textbox.png"]];
imgTxtQue.frame = CGRectMake(267, k, 490, 60);
// UIImageView *imgRatingBtn = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#""]];
// imgRatingBtn.frame = CGRectMake(633, RB, 102, 39);
//ratingButton1.hidden = NO;
ratingButton1 = [[UIButton alloc] initWithFrame:CGRectMake(633, RB, 102, 39)];
[ratingButton1 setImage:[UIImage imageNamed:#"rating_selected.png"] forState:UIControlStateNormal];
txtQuestions = [[UITextField alloc] initWithFrame:CGRectMake(296, j, 429, 24)];
//txtQuestions.tag = i;
[txtQuestions setTag:i];
NSLog(#"%d", txtQuestions.tag);
txtQuestions.delegate = self;
[txtQuestions setPlaceholder:[[appDelegate.questions objectAtIndex:i] objectForKey:#"question"]];
txtQuestions.borderStyle = UITextBorderStyleNone;
// txtQuestions.background = imgTxtQue.image;
[scrView addSubview:imgTxtQue];
[scrView addSubview:ratingButton1];
[scrView addSubview:txtQuestions];
j = j+50;
k = k+50;
RB = RB+50;
}
-(void)loadQuestion
{
txtQuestionOne.hidden=NO;
imgTxtQue.hidden=NO;
CGRect aFrame1 = imgTxtQue.frame;
aFrame1.size.width = aFrame1.size.width + 200;
// aFrame.size.height = newHeight;
NSString *strQue = [NSString stringWithFormat:#"%#",[[appDelegate.questions objectAtIndex:0] objectForKey:#"question"]];
CGSize newSize1 = [strQue sizeWithFont: [UIFont fontWithName: #"TrebuchetMS" size: 12] ];
if (strQue.length > 42) {
imgTxtQue.frame = CGRectMake(267, 413, newSize1.width+353, 60);
[txtQuestionOne setFrame:CGRectMake(294, 430, newSize1.width+350, 24)];
ratingButton1.frame = CGRectMake(855, 423, 102, 39);
ratingLabel1.frame = CGRectMake(900, 430, 42, 21);
}
[txtQuestionOne setPlaceholder:[[appDelegate.questions objectAtIndex:0] objectForKey:#"question"]];
appDelegate.dynamicQues =[[appDelegate.questions objectAtIndex:0] objectForKey:#"question"];
NSLog(#"current q1 %#", [[appDelegate.questions objectAtIndex:0] objectForKey:#"question"]);
if ([[[appDelegate.questions objectAtIndex:0] objectForKey:#"permission"] isEqualToString:#"1"]) {
ratingButton1.hidden=NO;
ratingLabel1.hidden=NO;
}
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
BOOL editing=YES;
for (UIImageView *img in ViewPost.subviews)
{
if ([img isKindOfClass:[UIImageView class]])
{
if (img.tag==textField.tag)
{
[img setImage:[UIImage imageNamed:#"Redtextbox.png"]];;
}
else{
if (img.tag!=0) {
if (img.tag != 10) {
[img setImage:[UIImage imageNamed:#"textbox.png"]];
}
else
{
[img setImage:[UIImage imageNamed:#"inputbox.png"]];
}
}
}
}
}
if (textField==txtcategory) {
[textField resignFirstResponder];
appDelegate.category=YES;
editing=NO;
touchflag=1;
[self performSelectorOnMainThread:#selector(CategoryListClick) withObject:nil waitUntilDone:YES];
//[self CategoryListClick];
return NO;
}
if (textField==txtsubcategory) {
appDelegate.category=NO;
editing=NO;
[self performSelectorOnMainThread:#selector(CategoryListClick) withObject:nil waitUntilDone:YES];
}
if (txtQuestions.tag) {
// if (textField==txtQuestions) {
editing=NO;
index=txtQuestions.tag;
NSLog(#"%d",txtQuestions.tag);
touchflag = 1;
[self performSelectorOnMainThread:#selector(loadAnswers) withObject:nil waitUntilDone:YES];
// }
}
-(void)loadAnswers{
if (touchflag==1) {
[Name resignFirstResponder];
[Email resignFirstResponder];
[txtCityCode resignFirstResponder];
[txtcategory resignFirstResponder];
[txtTitle resignFirstResponder];
[txtQuestions resignFirstResponder];
[txtsubcategory resignFirstResponder];
SubcategoryPopViewController *objPopview = [[SubcategoryPopViewController alloc]initWithNibName:#"SubcategoryPopViewController" bundle:nil];
objPopview.delegate = self;
objPopview.index=txtQuestions.tag;
popView =[[UIPopoverController alloc]initWithContentViewController:objPopview];
[self RescheduleTimer];
[popView presentPopoverFromRect:imgTxtQue.frame inView:ViewPost permittedArrowDirections:0 animated:NO];
[popView setPopoverContentSize:CGSizeMake(376, 260)];
}
}
-(void) didSelectAnswer{
touchflag=0;
[popView dismissPopoverAnimated:YES];
[self RescheduleTimer];
if (appDelegate.ansDynamic==YES) {
// txtQuestionOne.text=appDelegate.answerOne;
if (txtQuestions.tag) {
txtQuestions.text = appDelegate.answerDynamic;
}
appDelegate.ansDynamic=NO;
questionIndex=txtQuestions.tag;
if (![ratingButton1 isHidden]) {
ratingButton1.enabled=YES;
RatingPopViewController *objPopview = [[RatingPopViewController alloc]initWithNibName:#"RatingPopViewController" bundle:nil];
objPopview.index=txtQuestions.tag;
objPopview.checkedCell=ratingLabel1.text;
objPopview.QuestionText=appDelegate.answerDynamic;
objPopview.delegate=self;
popSweepStakes =[[UIPopoverController alloc]initWithContentViewController:objPopview];
[popSweepStakes presentPopoverFromRect:ratingButton1.frame inView:ViewPost permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
[popSweepStakes setPopoverContentSize:CGSizeMake(300, 400)];
}
return;
}
In the above code i get the tag correctly but when i use tag on other mathod then i only get the last textfields tag.
Thanks,
Rahul Virja

You overwrite txtQuestions variable.
Declare a class attribute like this NSMutableArray * _textFieldArray. (It's import declare a NSMutableArray object for add your textField).
In the init method of your class allocate and init your array: _textFieldArray = [[NSmutableArray alloc]init].
In your for statement use the following code for allocate a textField:
UItextField * txtQuestions = [[UITextField alloc] initWithFrame:CGRectMake(296, j, 429, 24)];
After setting all your property on txtQuestions object, add it to the array
[_textFieldArray addObject:txtQuestions]
When you need for a textfield use this code:
[_textFieldArray objectAtIndex:txtQuestions.tag]
Is it Clear?

Related

Identifying an UIImageView without tag

I have some generic generated ImageViews in a scrollView each of these images have 2 gestureRecognizer for single/double tapping on the ImageView. Now my problem is how to identify whether the ImageView was tapped for first or second time. In some scenarios it's easy to use the tag of an ImageView, but I have two different gestureRecognizer on each ImageView and every gestureRecognizer uses a different identification method based on the tag number, to identify the image.
Here I generate the ImageViews dynamically:
-(void) initLevels{
_level = [Level alloc];
_unit = [Unit alloc];
self->_units = [[NSMutableArray alloc] init];
_keys = [[NSMutableArray alloc] init]
;
int x = 0;
int y = 0;
int i = 0;
for (NSObject *object in self->_levels) {
if ([object isKindOfClass:_level.class] && i != 0) {
x = x + MARGIN_RIGHT + OBJECT_WIDTH;
y = 0;
}
else if ([object isKindOfClass:_unit.class]){
_unit = (Unit *) object;
[self->_units addObject:_unit.description];
UIImageView *imageView = [[UIImageView alloc] initWithImage:self.box];
[imageView setFrame:CGRectMake(x, y, OBJECT_WIDTH, BOX_HEIGHT)];
imageView.highlighted = TRUE;
imageView.tag = i; //when this is not outlined the gestureRecognizer for singleTapping works but on the other hand the double tap gestureRecognizer just works for the first object, because its' tag is set on 0.
UITapGestureRecognizer *doubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(unitDoubleTapped:)];
doubleTapGesture.numberOfTapsRequired = 2;
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:doubleTapGesture];
UITapGestureRecognizer *singleTapGesture =
[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(unitSingleTapped:)];
singleTapGesture.numberOfTapsRequired = 1;
//telling the singleTapGesture to fail the doubleTapGesture, so both doesn't fire at the same time
[singleTapGesture requireGestureRecognizerToFail:doubleTapGesture];
[imageView addGestureRecognizer:singleTapGesture];
UILabel *labelHeadline = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, 220, 20)];
[labelHeadline setFont:[UIFont boldSystemFontOfSize:12]];
labelHeadline.textAlignment = NSTextAlignmentCenter;
[labelHeadline setBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0]];
labelHeadline.text = _unit.headline;
labelHeadline.numberOfLines = 0;
[labelHeadline sizeToFit];
UILabel *labelPrice = [LabelUtils deepLabelCopy:labelHeadline withText:[NSString stringWithFormat:#"Price: %#",_unit.price] withFrame:NO];
[labelPrice setTextAlignment:NSTextAlignmentLeft];
[labelPrice setFrame:CGRectMake(labelHeadline.frame.origin.x, labelHeadline.frame.origin.y + labelHeadline.frame.size.height + 2, 220, 20)];
UILabel *labelCRM = [LabelUtils deepLabelCopy:labelHeadline withText:[NSString stringWithFormat:#"CRM: %#", _unit.crm] withFrame:NO];
[labelCRM setTextAlignment:NSTextAlignmentLeft];
[labelCRM setFrame:CGRectMake(labelPrice.frame.origin.x, labelPrice.frame.origin.y + labelPrice.frame.size.height + 2, 220, 20)];
UITextView *textView= [[UITextView alloc] initWithFrame:CGRectMake(0,0, OBJECT_WIDTH, OBJECT_HEIGHT)];
[textView addSubview:labelHeadline];
[textView addSubview:labelPrice];
[textView addSubview:labelCRM];
[textView setUserInteractionEnabled:NO];
[textView setEditable:NO];
textView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0];
textView.textAlignment = NSTextAlignmentLeft;
[imageView addSubview:textView];
[_scrollView addSubview:imageView];
y = y + MARGIN_BOTTOM + BOX_HEIGHT;
}
[self->_keys addObject:[NSNumber numberWithInt:i]];
i++;
}//remove the last keys which are to much in the _keys array
while ([self->_keys count] > ([self->_units count])) {
[_keys removeLastObject];
i--;
}
self.contents = [NSDictionary dictionaryWithObjects:self->_units forKeys:_keys];
}
Here is the code for the two gesture Recognizer
-(void)unitDoubleTapped:(UIGestureRecognizer *)gestureRecognizer{
self->_unitViewForDoubleTapIdentification = (UIImageView *)gestureRecognizer.view;
switch (self->_unitViewForDoubleTapIdentification.tag) {
case 0:
[_unitViewForDoubleTapIdentification setHighlightedImage:self.transparentBox];
self->_unitViewForDoubleTapIdentification.tag = 1;
break;
case 1:
[_unitViewForDoubleTapIdentification setHighlightedImage:self.box];
self->_unitViewForDoubleTapIdentification.tag = 0;
break;
default:
break;
}
}
and Here the singleTap
- (IBAction)unitSingleTapped:(id)sender {
[self dismissAllPopTipViews];
UIGestureRecognizer *gestureRecognizer = [UIGestureRecognizer alloc];
gestureRecognizer = (UIGestureRecognizer *)sender;
UIImageView *imageView = [[UIImageView alloc] init];
imageView = (UIImageView *)gestureRecognizer.view;
if (sender == _currentPopTipViewTarget) {
// Dismiss the popTipView and that is all
self.currentPopTipViewTarget = nil;
}
NSString *contentMessage = nil;
UIImageView *contentView = nil;
NSNumber *key = [NSNumber numberWithInt:imageView.tag];
id content = [self.contents objectForKey:key];
if ([content isKindOfClass:[NSString class]]) {
contentMessage = content;
}
else {
contentMessage = #"A large amount ot text in this bubble\najshdjashdkgsadfhadshgfhadsgfkasgfdasfdhasdkfgaodslfgkashjdfg\nsjfkasdfgkahdjsfghajdsfgjakdsfgjjakdsfjgjhaskdfjadsfgjdsfahsdafhjajdskfhadshfadsjfhadsjlfkaldsfhfldsa\ndsfgahdsfgajskdfgkafd";
}
NSArray *colorScheme = [_colorSchemes objectAtIndex:foo4random()*[_colorSchemes count]];
UIColor *backgroundColor = [colorScheme objectAtIndex:0];
UIColor *textColor = [colorScheme objectAtIndex:1];
CMPopTipView *popTipView;
if (contentView) {
popTipView = [[CMPopTipView alloc] initWithCustomView:contentView];
}
else {
popTipView = [[CMPopTipView alloc] initWithMessage:contentMessage];
}
[popTipView presentPointingAtView:imageView inView:self.view animated:YES];
popTipView.delegate = self;
popTipView.disableTapToDismiss = YES;
popTipView.preferredPointDirection = PointDirectionUp;
if (backgroundColor && ![backgroundColor isEqual:[NSNull null]]) {
popTipView.backgroundColor = backgroundColor;
}
if (textColor && ![textColor isEqual:[NSNull null]]) {
popTipView.textColor = textColor;
}
popTipView.animation = arc4random() % 2;
popTipView.has3DStyle = (BOOL)(arc4random() % 2);
popTipView.dismissTapAnywhere = YES;
[popTipView autoDismissAnimated:YES atTimeInterval:3.0];
[_visiblePopTipViews addObject:popTipView];
self.currentPopTipViewTarget = sender;
}
Hope you can help me, thanks in advance.
that's a lot of code to go through.. but I add more identifiers to UIViews (ie other than the tag identifier) by using obj-c runtime associative references..
So this is a category I attach to my UIView:
#import "UIView+Addons.h"
#import <objc/runtime.h>
#define kAnimationDuration 0.25f
#implementation UIView (Addons)
static char infoKey;
static char secondaryInfoKey;
-(id)info {
return objc_getAssociatedObject(self, &infoKey);
}
-(void)setInfo:(id)info {
objc_setAssociatedObject(self, &infoKey, info, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(id)secondaryInfo {
return objc_getAssociatedObject(self, &secondaryInfoKey);
}
-(void)setSecondaryInfo:(id)info {
objc_setAssociatedObject(self, &secondaryInfoKey, info, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#end
here is an example of my own code where I use the above to address a problem similar to the one you're facing:
if (![[view info] boolValue]) { // not selected?
self.moveToFolderNumber = [view secondaryInfo];
[view setInfo:#YES];
}

Keyboard is not resigning when tapping uipickerview

I have two UIPickerView and one UITextField in my search view. Everything was working fine when I Input/Select the two UIPickerView first and then I input UITextField.
But when I change the order like Inputting UITextField then selecting UIPickerView, the problem happening is keyboard is not resigned when I tap on the UIPickerViews.
Below is my code
- (void) textFieldDidBeginEditing: (UITextField *) textField
//---------------------------------------------------------------
{
if (textField == self.txtFieldPoetName) {
NSLog(#"sdsdfsd");
[textField resignFirstResponder];
if ( [self.poetNameArray count] > 0 ){
[self showPoetNamePicker];
}
}
else if (textField == self.txtFieldPoemType) {
NSLog(#"jdjdjdjd");
[textField resignFirstResponder];
if( [self.poemTypeArray count] > 0 ){
[self showPoemTypePicker];
}
}
}
- (void) textFieldDidEndEditing:(UITextField *)textField{
[textField resignFirstResponder];
}
- (void)showPoetNamePicker {
[self resignKeyboard];
startXMLParser_ = NO;
self.actionSheet = [[[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]autorelease];
self.actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
self.actionSheet.title = #"Poet Name";
self.poetNamePicker = [[[UIPickerView alloc] init]autorelease];
self.poetNamePicker.showsSelectionIndicator = YES;
self.poetNamePicker.dataSource = self;
self.poetNamePicker.delegate = self;
PickerType = 1;
//Set up the display frame
self.poetNamePicker.frame = CGRectMake(0, 35, 320, 120);
//Add the picker to the view
[self.actionSheet addSubview:self.poetNamePicker];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 6.0f, 50.0f, 27.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(hidePicker:) forControlEvents:UIControlEventValueChanged];
[self.actionSheet addSubview:closeButton];
[closeButton release];
[self.actionSheet showInView:self.appDelegate.tabBarController.view];
[self.actionSheet setBounds:CGRectMake(0, 0, 320, 385)];
}
- (void)showPoemTypePicker {
[self resignKeyboard];
startXMLParser_ = YES;
self.actionSheet = [[[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]autorelease];
self.actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
self.actionSheet.title = #"Poem Type";
self.poemTypePicker = [[[UIPickerView alloc] init]autorelease];
self.poemTypePicker.showsSelectionIndicator = YES;
self.poemTypePicker.dataSource = self;
self.poemTypePicker.delegate = self;
PickerType = 2;
//Set up the display frame
self.poemTypePicker.frame = CGRectMake(0, 35, 320, 120);
//Add the picker to the view
[self.actionSheet addSubview:self.poemTypePicker];
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 6.0f, 50.0f, 27.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:#selector(hidePicker:) forControlEvents:UIControlEventValueChanged];
[self.actionSheet addSubview:closeButton];
[closeButton release];
[self.actionSheet showInView:self.appDelegate.tabBarController.view];
[self.actionSheet setBounds:CGRectMake(0, 0, 320, 385)];
}
- (void)hidePicker:(id)sender
{
if( PickerType == 1 )
{
if ( [self.poetNameArray count] > 0 &&
[self.txtFieldPoetName.text length] == 0 )
{
//self.poemTypeId = #"";
self.txtFieldPoetName.text = [self.poetNameArray objectAtIndex:0];
self.poetTypeId = [self.poetTypeIdArray objectAtIndex:0];
}
}
else if( PickerType == 2 )
{
if ( [self.poemTypeArray count] > 0 &&
[self.txtFieldPoemType.text length] == 0)
{
//self.poetTypeId = #"";
self.txtFieldPoemType.text = [self.poemTypeArray objectAtIndex:0];
self.poemTypeId = [self.poemTypeIdArray objectAtIndex:0];
}
}
[self resignFirstResponder];
self.actionSheet.hidden = YES;
[self.actionSheet dismissWithClickedButtonIndex:1 animated:YES];
if (startXMLParser_)
{
[self performSelector:#selector(startXMLParsing) withObject:nil afterDelay:0.5];
}
}
-(void)resignKeyboard{
[self.txtFieldPoetName resignFirstResponder];
[self.txtFieldPoemName resignFirstResponder];
[self.txtFieldPoemType resignFirstResponder];
}
EDIT :
- (void) textFieldShouldBeginEditing: (UITextField *) textField
{
if (textField == self.txtFieldPoetName)
{
if( [self.poemNameArray count] > 0 ){
[self showPoetNamePicker];
}
return NO;
}
else if (textField == self.txtFieldPoemType)
{
if( [self.poemTypeArray count] > 0 ){
[self showPoemTypePicker];
}
return NO;
}
else
{
return YES;
}
}
In showPoetNamePicker add this:
- (void)showPoetNamePicker
{
[self.view endEditing:YES];
........
........
}
Have you linked to delegate properly?
Try changing this method,
-(void)resignKeyboard{
[self.view endEditing:YES];
}
I know this is an old question, but I think it should be mentioned ... I think the better solution is to use inputView (and optionally inputAccessoryView) of each textField. Something like that:
-(void)setupTextFields{
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
toolbar.items = [NSArray arrayWithObjects:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(endEditing)], nil];
self.txtFieldPoetName.inputView = self.poetNamePicker;
self.txtFieldPoetName.inputAccessoryView = toolbar;
self.txtFieldPoemType.inputView = self.poetTypePicker;
self.txtFieldPoemType.inputAccessoryView = toolbar;
}
-(void)endEditing{
[self.txtFieldPoetName resignFirstResponder];
[self.txtFieldPoemType resignFirstResponder];
}
OS ensures switching picker for keyboard and vice versa (it's displayed in same view as keyboard I guess).

UIScrollView content not updating

I have UIScrollView in stage and paging functionality enabled to move back/next.
I have added 10 subviews in UIScrollView. When I have modify the content inside the subviews, then it is not reflected in UIScrollView.
Book.m
- (void) initializeWithXML:(NSString *)XMLURLString {
NSData *xmlData;
NSString *url;
if ( ![applicationData getMode] ) {
resourceRootURL = [[applicationData getAssetsPath] stringByAppendingPathComponent:#"phone/"];
} else {
resourceRootURL = [applicationData getAssetsPath];
}
url = [resourceRootURL stringByAppendingPathComponent:XMLURLString];
//NSLog(#"Root URL...%#", url);
if ([url rangeOfString:#"http"].location != NSNotFound) {
xmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
} else {
xmlData = [NSData dataWithContentsOfFile:url];
}
bookContentArray = [self grabXML:xmlData andQuery:#"//page"];
// view controllers are created lazily
// in the meantime, load the array with placeholders which will be replaced on demand
NSMutableArray *controllers = [[NSMutableArray alloc] init];
for (unsigned i = 0; i < kNumberOfPages; i++) {
[controllers addObject:[NSNull null]];
}
self.viewControllers = controllers;
[controllers release];
for(int i = 0; i < [bookContentArray count]; i++) {
[viewControllers addObject:[NSNull null]];
}
if ( isTwoPage ) {
kNumberOfPages = [bookContentArray count];
} else {
kNumberOfPages = [bookContentArray count]/2;
}
CGRect pagingScrollViewFrame = [self frameForPagingScrollView];
scrollView = [[UIScrollView alloc] initWithFrame:pagingScrollViewFrame];
scrollView.pagingEnabled = YES;
scrollView.backgroundColor = [UIColor blackColor];
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.contentSize = [self contentSizeForPagingScrollView];
scrollView.contentSize = CGSizeMake(scrollView.contentSize.width,scrollView.frame.size.height);
scrollView.delegate = self;
self.view = scrollView;
kNumberOfPages = [bookContentArray count];
pageControl.numberOfPages = kNumberOfPages;
pageControl.currentPage = 0;
// pages are created on demand
// load the visible page
// load the page on either side to avoid flashes when the user starts scrolling
[self loadPage:0];
}
- (void) loadPage:(int)number {
// Calculate which pages are visible
int firstNeededPageIndex = MAX(number-1, 0);
int lastNeededPageIndex = MIN(number+1, [viewControllers count] - 1);
//NSLog(#"%d,%d",firstNeededPageIndex,lastNeededPageIndex);
// Recycle no-longer-visible pages
for(int i = 0; i < [viewControllers count]; i++) {
ImageScrollView *page = [viewControllers objectAtIndex:i];
if ((NSNull *)page != [NSNull null]) {
if (page.index < firstNeededPageIndex || page.index > lastNeededPageIndex) {
//NSLog(#"removed page %d", page.index);
[page removeImages];
[page removeFromSuperview];
page = nil;
[viewControllers replaceObjectAtIndex:i withObject:[NSNull null]];
}
}
}
// load the visible page and the page on either side of it (to avoid flashes when the user starts scrolling)
if ( number == 0 ) {
[self loadScrollViewWithPage:number];
[self loadScrollViewWithPage:number+1];
} else if ( number == [bookContentArray count]-1) {
[self loadScrollViewWithPage:number-1];
[self loadScrollViewWithPage:number];
} else {
[self loadScrollViewWithPage:number-1];
[self loadScrollViewWithPage:number];
[self loadScrollViewWithPage:number+1];
}
}
- (void)loadScrollViewWithPage:(int)page {
ImageScrollView *pageController = [[ImageScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
pageController.index = page;
pageController.myDelegate = self;
pageNumber = pageControl.currentPage;
pageController.isTwoPage = isTwoPage;
if ( pageNumber == page ) {
pageController.isCurrentPage = YES;
} else {
pageController.isCurrentPage = NO;
}
[controller setImageURL:leftURL andRightURL:rightURL andPriority:0];
[scrollView addSubview:controller];
}
ImageScrollView.m
- (void) setImageURL:(NSString *)leftURL andRightURL:(NSString *)rightURL andPriority:(int)priority {
[imageView removeFromSuperview];
[imageView release];
imageView = nil;
if ( !isTwoPage ) {
imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
} else {
imageView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
}
if ( isTwoPage ) {
leftImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
leftImage.view.frame = CGRectMake(0, 43, 512, 682);
rightImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
rightImage.view.frame = CGRectMake(512, 43, 512, 682);
} else {
leftImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
leftImage.view.frame = imageView.frame;
rightImage = [[FBEPage alloc] initWithNibName:#"FBEPage" bundle:nil];
rightImage.view.frame = imageView.frame;
}
leftImage.delegate = self;
rightImage.delegate = self;
if ( isFirstPage ) {
[leftImage.view setHidden:YES];
[rightImage.view setHidden:NO];
} else if ( isLastPage ) {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:YES];
} else if ( !isTwoPage ) {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:YES];
} else {
[leftImage.view setHidden:NO];
[rightImage.view setHidden:NO];
}
imageView.backgroundColor = [UIColor whiteColor];
[self addSubview:imageView];
[leftImage setImageURL:leftURL];
[rightImage setImageURL:rightURL];
[imageView addSubview:leftImage.view];
[imageView addSubview:rightImage.view];
}
FBPage.m
- (void) setImageURL:(NSString *)url {
imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(0, 0, 768, 1024);
imageView.center = self.view.center;
imageView.userInteractionEnabled = TRUE;
[container addSubview:imageView];
[activityIndicator setHidden:YES];
if ([url rangeOfString:#"http"].location != NSNotFound) {
SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:[NSURL URLWithString:url]];
if (cachedImage) {
[imageView setImage:cachedImage];
isImageLoaded = YES;
[self addScrollView];
} else {
[activityIndicator setHidden:NO];
[activityIndicator startAnimating];
[manager downloadWithURL:[NSURL URLWithString:url] delegate:self options:0 success:^(UIImage *image){
[activityIndicator stopAnimating];
[activityIndicator setHidden:YES];
[imageView setImage:image];
isImageLoaded = YES;
[self addScrollView];
} failure:nil];
}
imageURL = url;
} else {
imageView.image = [UIImage imageWithContentsOfFile:url];
isImageLoaded = YES;
[self addScrollView];
}
}
- (void) setHotspotURL:(NSString *)URL {
[hotspot.view removeFromSuperview];
[hotspot release];
hotspot = nil;
hotspot = [[FBHotspot alloc] initWithNibName:#"FBHotspot" bundle:nil];
hotspot.view.frame = CGRectMake(0, 0, 768, 1024);
hotspot.delegate = self;
[hotspot setURL:URL];
[container addSubview:hotspot.view];
}
**When I am calling setHotspotURL and it’s not updating to the scrollview.**
Make sure you inserted your content at the correct position within the scroll view. Set the contentSize accordingly. Try to call layoutSubviews of the UIScrollView instance

Multiple rating bars with DYRateView

I have a view with 3 ratings bars on it.
How do I tell them apart in the code? Right now if I change the top one it sees it fine but if I then change either or the other two it cannot separate them from one another.
The code is from git https://github.com/dyang/DYRateView
- (void)changedToNewRate:(NSNumber *)rate {
NSString *rating = [NSString stringWithFormat:#"Rate: %d", rate.intValue];
NSLog(#"rating: %#",rating);
}
This is where is sees the changing.
And this is the .m file
#import "Survey.h"
#implementation Survey
#synthesize btnSubmit;
- (void)setUpEditableRateView {
DYRateView *rateService = [[DYRateView alloc] initWithFrame:CGRectMake(0, 55, self.view.bounds.size.width, 40) fullStar:[UIImage imageNamed:#"StarFullLarge#2x.png"] emptyStar:[UIImage imageNamed:#"StarEmptyLarge#2x.png"]];
rateService.padding = 20;
rateService.alignment = RateViewAlignmentCenter;
rateService.editable = YES;
rateService.delegate =self;
[scroller addSubview:rateService];
[rateService release];
DYRateView *rateFood = [[DYRateView alloc] initWithFrame:CGRectMake(0, 130, self.view.bounds.size.width, 40) fullStar:[UIImage imageNamed:#"StarFullLarge#2x.png"] emptyStar:[UIImage imageNamed:#"StarEmptyLarge#2x.png"]];
rateFood.padding = 20;
rateFood.alignment = RateViewAlignmentCenter;
rateFood.editable = YES;
rateFood.delegate = self;
[scroller addSubview:rateFood];
[rateFood release];
DYRateView *rateCleanliness = [[DYRateView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 40) fullStar:[UIImage imageNamed:#"StarFullLarge#2x.png"] emptyStar:[UIImage imageNamed:#"StarEmptyLarge#2x.png"]];
rateCleanliness.padding = 20;
rateCleanliness.alignment = RateViewAlignmentCenter;
rateCleanliness.editable = YES;
rateCleanliness.delegate = self;
[scroller addSubview:rateCleanliness];
[rateCleanliness release];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)changedToNewRate:(NSNumber *)rate {
NSString *rating = [NSString stringWithFormat:#"Rate: %d", rate.intValue];
NSLog(#"rating: %#",rating);
}
- (IBAction)btnSubmit:(id)sender{
}
-(IBAction)mainMenu{
[self dismissModalViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320, 600)];
scroller.BackgroundColor = [UIColor clearColor];
[self setUpEditableRateView];
[btnSubmit useGreenConfirmStyle];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
UPDATE
ok with the new code I added an if statement
- (void)rateView:(DYRateView *)rateView changedToNewRate:(NSNumber *)rate {
NSString *barName = [NSString stringWithFormat:#"%#", rateView];
if (barName == #"rateView1") {
NSString *bar1 = [NSString stringWithFormat:#"Rate: %d", rate.intValue];
NSLog(#"bar 1: %#",bar1);
}else if(barName == #"rateView2"){
NSString *bar2 = [NSString stringWithFormat:#"Rate: %d", rate.intValue];
NSLog(#"bar 2: %#",bar2);
}else{
NSLog(#"NO BAR: %#",barName);
}
self.rateLabel.text = [NSString stringWithFormat:#"Rate: %d", rate.intValue];
}
It works however Instead of getting back "rateView1" or 2 or 3. I get back
NO BAR: <DYRateView: 0x78229b0; frame = (0 40; 320 20); opaque = NO; layer =
<CALayer:0x7824900>>
Which is correct in nature but I was hoping for the name of the rateView such as "rateView1"
SOLUTION:
in .h file #property your 2 or 3 bars
#property(nonatomic, retain) DYRateView *rateView1,*rateView2;
in .m #synthesize them
#synthesize rateView1;
#synthesize rateView2;
then
(void)setUpEditableRateView {
rateView1 = [[DYRateView alloc] initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, 20) fullStar:[UIImage imageNamed:#"StarFullLarge.png"] emptyStar:[UIImage imageNamed:#"StarEmptyLarge.png"]];
rateView1.padding = 20;
rateView1.alignment = RateViewAlignmentCenter;
rateView1.editable = YES;
rateView1.delegate = self;
[self.view addSubview:rateView1];
[rateView1 release];
rateView2 = [[DYRateView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 20) fullStar:[UIImage imageNamed:#"StarFullLarge.png"] emptyStar:[UIImage imageNamed:#"StarEmptyLarge.png"]];
rateView2.padding = 20;
rateView2.alignment = RateViewAlignmentCenter;
rateView2.editable = YES;
rateView2.delegate = self;
[self.view addSubview:rateView2];
[rateView2 release];
// Set up a label view to display rate
self.rateLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 80, self.view.bounds.size.width, 20)] autorelease];
self.rateLabel.textAlignment = UITextAlignmentCenter;
self.rateLabel.text = #"Tap above to rate";
[self.view addSubview:self.rateLabel];
}
Then Finally
- (void)rateView:(DYRateView *)rateView changedToNewRate:(NSNumber *)rate {
if (rateView == rateView1) {
NSString *bar1 = [NSString stringWithFormat:#"Rate: %d", rate.intValue];
NSLog(#"bar 1: %#",bar1);
}else if(rateView == rateView2){
NSString *bar2 = [NSString stringWithFormat:#"Rate: %d", rate.intValue];
NSLog(#"bar 2: %#",bar2);
}else{
NSLog(#"NO BAR: %#",rateView);
}
self.rateLabel.text = [NSString stringWithFormat:#"Rate: %d", rate.intValue];
}
Thanks for using DYRateView!
The current version of DYRateView doesn't support listening to multiple instances all at the same time, but that doesn't mean that we can't do that. :)
If you don't mind updating the source code, you can find a method named notifyDelegate in DYRateView.m, and change [self.delegate performSelector:#selector(changedToNewRate:) withObject:[NSNumber numberWithFloat:self.rate]] to something like [self.delegate performSelector:#selector(rateView:changedToNewRate:) withObject:self withObject:[NSNumber numberWithFloat:self.rate]]. In this way you are able to pass the rateView itself as a parameter to its listener.
I haven't tried the above code yet as I don't have access to my Mac at this point, but I think this should give you an idea regarding how to achieve your goal.
I just set the tag for each of my rateviews to tell them apart, so in your example, I add a tag to each one:
#define serviceTag 1
#define foodTag 2
#define cleanlinessTag 3
- (void)setUpEditableRateView {
DYRateView *rateService = [[DYRateView alloc] initWithFrame:CGRectMake(0, 55, self.view.bounds.size.width, 40) fullStar:[UIImage imageNamed:#"StarFullLarge#2x.png"] emptyStar:[UIImage imageNamed:#"StarEmptyLarge#2x.png"]];
rateService.padding = 20;
rateService.alignment = RateViewAlignmentCenter;
rateService.editable = YES;
rateService.tag = serviceTag;
rateService.delegate =self;
[scroller addSubview:rateService];
[rateService release];
DYRateView *rateFood = [[DYRateView alloc] initWithFrame:CGRectMake(0, 130, self.view.bounds.size.width, 40) fullStar:[UIImage imageNamed:#"StarFullLarge#2x.png"] emptyStar:[UIImage imageNamed:#"StarEmptyLarge#2x.png"]];
rateFood.padding = 20;
rateFood.alignment = RateViewAlignmentCenter;
rateFood.editable = YES;
rateFood.tag = foodTag;
rateFood.delegate = self;
[scroller addSubview:rateFood];
[rateFood release];
DYRateView *rateCleanliness = [[DYRateView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 40) fullStar:[UIImage imageNamed:#"StarFullLarge#2x.png"] emptyStar:[UIImage imageNamed:#"StarEmptyLarge#2x.png"]];
rateCleanliness.padding = 20;
rateCleanliness.alignment = RateViewAlignmentCenter;
rateCleanliness.editable = YES;
rateCleanliness.delegate = self;
rateCleanliness.tag = cleanlinessTag;
[scroller addSubview:rateCleanliness];
[rateCleanliness release];
}
Then, in the delegate method, you can just check the tag:
- (void)rateView:(DYRateView *)rateView changedToNewRate:(NSNumber *)rate {
switch (rateView.tag) {
case foodTag:
// Do something for food
break;
case serviceTag:
// Do something for service
break;
case cleanlinessTag:
// Do something for cleanliness
break;
default:
break;
}
}

UIButton failed to call functions?

I have a viewcontroller(mainViewController) and view class(UIView class, mainView). mainViewController, I called a method "generateView" from mainView class. Here is the code:
Method 1:
- (void) generateView {
container = [[UIScrollView alloc] initWithFrame: CGRectMake(10, 10, 500, 300)];
container.backgroundColor = [UIColor clearColor];
container.contentSize = CGSizeMake(500,1200);
container.showsVerticalScrollIndicator = YES;
[container setScrollEnabled:TRUE];
container.backgroundColor = [UIColor redColor];
int row = 0;
int col = 0;
for (int i = 0; i < 5; i++) {
if(i % 4 == 0 && i != 0){
row++;
col = 0;
}
UIButton *b = [[UIButton alloc] initWithFrame: CGRectMake(col*120, row*120, 100, 100)];
NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:#"%#/images/items/%i.png", HOSTADDR, i ]]];
[b setBackgroundImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
[b setBackgroundColor: [UIColor colorWithRed:0x42/255.0f green:0x30/255.0f blue:0x27/255.0f alpha:1]];
[b setTag: i];
[b addTarget:self action:#selector(testFunction:) forControlEvents:UIControlEventTouchUpInside];
[b setEnabled:TRUE];
[b setUserInteractionEnabled:TRUE];
[container addSubview:b];
col++;
}
[self addSubview:container];
}
- (void) testFunction: (id) sender {
NSLog(#"Function called. Sender tag number: %i", [sender tag]);
}
Method 2:
- (void) generateView {
container = [[UIScrollView alloc] initWithFrame: CGRectMake(10, 10, 500, 300)];
container.backgroundColor = [UIColor clearColor];
container.contentSize = CGSizeMake(500,1200);
container.showsVerticalScrollIndicator = YES;
[container setScrollEnabled:TRUE];
container.backgroundColor = [UIColor redColor];
int row = 0;
int col = 0;
for (int i = 0; i < 5; i++) {
if(i % 4 == 0 && i != 0){
row++;
col = 0;
}
UIButton *b = [[UIButton alloc] initWithFrame: CGRectMake(col*120, row*120, 100, 100)];
NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:#"%#/images/items/%i.png", HOSTADDR, i ]]];
[b setBackgroundImage:[UIImage imageWithData:imageData] forState:UIControlStateNormal];
[b setBackgroundColor: [UIColor colorWithRed:0x42/255.0f green:0x30/255.0f blue:0x27/255.0f alpha:1]];
[b setTag: i];
[b addTarget:mainViewController action:#selector(updateData:) forControlEvents:UIControlEventTouchUpInside];
[b setEnabled:TRUE];
[b setUserInteractionEnabled:TRUE];
[container addSubview:b];
col++;
}
[self addSubview:container];
}
What I really need is method 2, cuz I want to call the updateData in mainViewController, but no matter how i tried, the button is not calling the function thats supposed to be called. Any idea?
EDIT:
This is how I called the view class
- (void) loadContent: (id) sender {
[self removeSubmenus];
switch ([sender tag]) {
case 2001:
{
MainView *mainView = [[MainView alloc] initWithSubmenu:CGRectMake(420, 85, 0, 0)];
[self.view addSubview:mainView];
break;
}
default:
break;
}
}
You should set delaysContentTouches to YES on the UIScrollView.
You're also leaking a bunch of memory. Make sure you're releasing both the UIButton (b) and NSData (imageData) objects as soon as you're done with them.
--
EDIT: I tried your code and it works fine. But it seems you wanted all this code in a UIView, so I threw together a quick sample.
TestView.h
#import <UIKit/UIKit.h>
#interface TestView : UIView {}
#end
TestView.m
#import "TestView.h"
#implementation TestView
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UIScrollView *container = [[UIScrollView alloc] initWithFrame: CGRectMake(10, 10, 500, 300)];
int row = 0;
int col = 0;
for (int i = 0; i < 5; i++) {
if(i % 4 == 0 && i != 0){
row++;
col = 0;
}
UIButton *b = [[UIButton alloc] initWithFrame: CGRectMake(col*120, row*120, 100, 100)];
[b setBackgroundColor: [UIColor colorWithRed:0x42/255.0f green:0x30/255.0f blue:0x27/255.0f alpha:1]];
[b setTag: i];
[b addTarget:self action:#selector(testFunction:) forControlEvents:UIControlEventTouchUpInside];
[container addSubview:b];
[b release];
col++;
}
[self addSubview:container];
}
return self;
}
- (void) testFunction: (id) sender {
NSLog(#"Function called. Sender tag number: %i", [sender tag]);
}
- (void)dealloc {
[super dealloc];
}
#end
TestController.h
#interface TestController : UIViewController { }
#end
TestController.m:
#import "TestController.h"
#import "TestView.h"
#implementation TestController
- (void) generateView {
TestView *tv = [[TestView alloc]initWithFrame:[self.view frame]];
[self.view addSubview:tv];
[tv release];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self generateView];
}
- (void)dealloc {
[super dealloc];
}
#end
Try adding this code to your method
container.delaysContentTouches = NO;
container.canCancelContentTouches = NO;
Pls modify your code to avoid memory leaks. Make sure you delete objects that you take ownership of.
Try this,
[b addTarget:mainViewController.view action:#selector(updateData:) forControlEvents:UIControlEventTouchUpInside];