Load url selected object in UIWebView (iOS) - ios

I want to open a UIWebView with a url based on the index of a custom object from an array. When I tap a button, based upon the index of objects in an array, I need for the UIWebView to open up that particular url. The problem is that when I open the webview, it will only load the url of the first object (at index 0) in the array. Here is my code:
.m:
//Here's a snippet of my viewDidLoad method.
-(void)viewDidLoad{
[super viewDidLoad];
buyButton = [[UIButton alloc]init];
buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
buyButton.frame = CGRectMake(228, 8, 25, 25);
[buyButton setBackgroundImage:[UIImage imageNamed:#"buy-now.png"] forState:UIControlStateNormal];
[buyButton addTarget:self action:#selector(buyBttnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buyButton];
int count = [self->myArray count];
for (int index = 0; index < count; index++)
{
DomainModel *eachObject = [self->myArray objectAtIndex:buyButton.tag];
buyButton.tag = index;
urlString = [NSString stringWithFormat:#"%#",eachObject.url];
NSLog(#"Selected index: %d",urlString);
}
-(void)buyBttnPressed:(id)sender{
UIWebView *buyView = [[UIWebView alloc] initWithFrame:CGRectMake(9,132,305,368)];
buyView.backgroundColor = [UIColor whiteColor];
buyView.scalesPageToFit = YES;
buyView.delegate = self;
[self.view addSubview:buyView];
UIButton *cancelBttn = [[UIButton alloc]init];
cancelBttn = [UIButton buttonWithType:UIButtonTypeCustom];
cancelBttn.frame = CGRectMake(3, 5, 25, 25);
[cancelBttn setBackgroundImage:[UIImage imageNamed:#"delete.png"] forState:UIControlStateNormal];
[buyView addSubview:cancelBttn];
[buyView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
How do I fix the loop so that when the buyBttn is pressed, the the url associated with the associated index loads?

it seems that if you call this code on buyBtnPressed, then the TAG of the button will be set to the self.myArray.count -1 always (you're looping but setting it always on the same button).
so current will always point to the same item - the last one in the list.
i think this:
int i = 0;
for (MyObject *obj in self->myArray)
{
i++;
btn.tag = i;
}
should be done on viewDidLoad, or wherever you fill your array in.

Related

objective c How to access and change dynamically created button background color in a uiscrollview

As you can see in the screenshot I have created multiple buttons dynamically in a UI scroll view. Every button actually holds a date action upon which specific events to that date loads into table. UI Scroll view is global element in the code which can be accessed by any method in the controller.
What I wanted now is access a button (when someone chooses a date to see events on that date) in that scrollview and change its background color. For your convenience I have added the whole code for date and scrollview. Please also note that data are loading from remote server.
Link to the screenshot is as follows
Please forgive me if you see anything wrong in typing. This is my first question in stack.
luckyDateScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(22, 2, lucky_screen_width-45, 23)];
luckyDateScrollView.showsHorizontalScrollIndicator = NO;
CGFloat paperWidth = 50;
int numberOfPapers = [dateRanges count];
for (int i=0; i<[dateRanges count]; i++) {
//NSLog(#"%d: %#", i, dateRange[i]);
//php like exploding with separator |
NSArray *dateString = [dateRanges[i] componentsSeparatedByString:#"|"];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake( 5+ (paperWidth+4) * i , 0, paperWidth, luckyDateScrollView.bounds.size.height)];
btn.titleLabel.font = [UIFont fontWithName:#"Arial" size:11.0f];
[btn setTitle: [NSString stringWithFormat:#"%#",dateString[0]] forState:UIControlStateNormal];
//dateResendButton
[btn setTag:i];
[btn addTarget:self
action:#selector(byDateFilterButton:)
forControlEvents:UIControlEventTouchUpInside];
if([today isEqualToString:dateString[1]]){
[GlobalMethods makeRoundedView:btn WithColorString:#"#ffffff" BGColorString:#"#004D00"];
}else{
[GlobalMethods makeRoundedView:btn WithColorString:#"#ffffff" BGColorString:GlobalVariables.initGV.BlackColor];
}
//[GlobalMethods makeRoundedView:btn WithColorString:#"#ffffff" BGColorString:#"#FF0000"];
[luckyDateScrollView addSubview:btn];
}
contentSize = CGSizeMake( 10 + (paperWidth+4) * numberOfPapers, luckyDateScrollView.bounds.size.height);
luckyDateScrollView.contentSize = contentSize;
// [GlobalMethods makeRoundedView:aScrollView WithColorString:#"#ffffff" BGColorString:GlobalVariables.initGV.BlackColor];
[dateUIView addSubview:luckyDateScrollView];
luckyDateBtnLeftScroll = [[UIButton alloc] initWithFrame:CGRectMake(5, 2, 20, 23)];
[luckyDateBtnLeftScroll setBackgroundImage:[UIImage imageNamed:#"arrow_g_icon_left"] forState:UIControlStateNormal];
[luckyDateBtnLeftScroll addTarget:self action:#selector(dateSetScrollToLeft:) forControlEvents:UIControlEventTouchUpInside];
[dateUIView addSubview:luckyDateBtnLeftScroll];
luckyDateBtnRightScroll = [[UIButton alloc] initWithFrame:CGRectMake(lucky_screen_width-25, 2, 20, 23)];
[luckyDateBtnRightScroll setBackgroundImage:[UIImage imageNamed:#"arrow_g_icon"] forState:UIControlStateNormal];
[luckyDateBtnRightScroll addTarget:self action:#selector(dateSetScrollToRight:) forControlEvents:UIControlEventTouchUpInside];
[dateUIView addSubview:luckyDateBtnRightScroll];
//ViewCollapse.backgroundColor = [UIColor colorWithCGColor: #"#FFD80D"];
If I understand your question correctly you can simply change the background of your button in the byDateFilterButton: method. It should have a sender parameter which would be the button in question.
If you also need to change the background of the other buttons back to normal, you need to keep a reference to them, probably in an array (or dictionary - if you have many buttons and need fast access).
I modified byDateFilterButton method in a following way. Thanks to this SO post Access all buttons in uiscollview
for (id obj in luckyDateScrollView.subviews) {
NSString *classStr = NSStringFromClass([obj class]);
if ([classStr isEqualToString:#"UIButton"]) {
UIButton *button = (UIButton*)obj; //NSLog(#"buttonn tag %ld",(long)button.tag);
if(sender.tag == button.tag){ // currently selected button
[GlobalMethods makeRoundedView:button WithColorString:#"#ffffff" BGColorString:#"#555454"];
}
else{ // the rest of buttons
[GlobalMethods makeRoundedView:button WithColorString:#"#ffffff" BGColorString:GlobalVariables.initGV.BlackColor];
}
}
}

UIButton addTarget does not work in subclass of UIViewController

This is MyViewController.m
actionNames[0] = #"hoge";
actionNames[1] = #"piyo";
NSMutableArray *actionConts = [[NSMutableArray alloc] init];
for(int i=0; i<[actionNames count]; i++){
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setFrame:CGRectMake(DEVICE_WIDTH/3 * i, 0, DEVICE_WIDTH/3, 45)];
// actionNames is NSMutableArray
[btn setTitle:actionNames[i] forState:UIControlStateNormal];
[btn setUserInteractionEnabled:YES];
actionConts[i] = btn;
}
// ↓ does not work
[actionConts[1] addTarget:self action:#selector(follow) forControlEvents:UIControlEventTouchUpInside];
// actionSubs is NSMutableArray
for(int i=0; i<[actionConts count]/3; i++){
actionSubs[i] = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 45)];;
}
for(int i=0; i<[actionConts count]; i++){
int sub = floor( (float)i / 3.0 );
[actionSubs[sub] addSubview:actionConts[i]];
}
UIScrollView *actionScr = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 150, 320, 45)];
[actionScr setPagingEnabled:YES];
[actionScr setShowsHorizontalScrollIndicator:NO];
[actionScr setBounces:NO];
actionScr.contentSize = CGSizeMake([actionSubs count] * DEVICE_WIDTH, 45);
for(int i=0; i<[actionSubs count]; i++){
[actionScr addSubview:actionSubs[i]];
}
Create a button. => actionConts[n]
addTarget to actionConts[1]
actionSubs[] alloc
addSubView actionConts[n] to actionSubs[n]
addSubView actionSubs[n] to actionScr
(addSubView actionScr to drw)
(drw is a UIView instance. "IBOutlet UIView *drw;")
When I click this button, It does not work... (´;ω;`)
Please help me.
Thanks.
Your code works fine to me. I only replaced some lines:
[actionConts[1] addTarget:self action:#selector(follow) forControlEvents:UIControlEventTouchUpInside];
replaced with:
for (UIButton *button in actionConts) {
[button addTarget:self action:#selector(follow) forControlEvents:UIControlEventTouchUpInside];
}
that allows adding the actions to each button from array.
Also I replaced for-loop condition from:
for(int i=0; i<[actionConts count]/3; i++)
to:
for(int i=0; i<ceil((float)[actionConts count]/3.0); i++)
that allows having at least one superview for buttons.
Now follow is a method that will fire for each your button. You can set UIButton.tag property to clarify which one is pressed.

get tag of UIButton and those with smaller tags

I'm not understanding if what I'm trying to do is possible or not.
I am creating buttons in for loop:
CGRect rect2 = CGRectMake(50, 230, 40, 40);
for (int i = 0; i<5; i++) {
NSString *stringI = [NSString stringWithFormat:#"%d",i+1];
NSString *stringItouch = [NSString stringWithFormat:#"%dselected",i+1];
UIButton *button = [[UIButton alloc] init];
[button setBackgroundImage:[UIImage imageNamed:stringI] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:stringItouch] forState:UIControlStateSelected];
[button addTarget:self action:#selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i+1;
button.frame = rect2;
rect2.origin.x = rect2.origin.x + 45;
[scrollView addSubview:button];
}
and after in method touchButton i get the tag of touched button
-(void)touchButton:(id)sender {
UIButton *buttonSender = sender;
buttonSender.selected = YES;
NSLog(#"button tag %#",buttonSender.tag);
for (int i = buttonSender.tag-1; i>0; i--) {
NSLog(#"int = %d",i);
//for example if buttonSender.tag is 4, in this way i have 3,2,1
}
}
in the last loop i want to select the buttons that have the tag less than that touched (in this case 3,2,1)
is it possible or not???
thanks everybody
All you need is viewWithTag: like so:
-(void)touchButton:(id)sender {
UIButton *buttonSender = sender;
buttonSender.selected = YES;
NSLog(#"button tag %#",buttonSender.tag);
for (int i = buttonSender.tag-1; i>0; i--) {
NSLog(#"int = %d",i);
//for example if buttonSender.tag is 4, in this way i have 3,2,1
/* Add this line */
UIButton *tempButton = (UIButton *)[scrollView viewWithTag:i];
}
}

how to get button title using for loop

//here take a array with letters
_englishArray = [[NSMutableArray alloc]initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z", nil];
int i=0;
float x=5,y=13;
//add array elements as button title
for (i=0; i< [_englishArray count]; i++) {
button.tag=i+1;
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[_englishArray objectAtIndex:i] forState:UIControlStateNormal];
button.frame= CGRectMake(x, y, 29, 30);
button.titleLabel.textColor =[UIColor whiteColor];
x= x+31;
if (x==320 || x>310) {
x=5;
y=y+40;
}
[_keyboardImageView addSubview:button];
}
// and i add the each button to _keyboardImageView and set title as _english arry elements ...and the get buttons curren title for this code...
//here get the button title useing for loop
for (int j=0;j<=[_englishArray count]; j++) {
// here get button from image view by useing tag
butt = (UIButton *)[_keyboardImageView viewWithTag:j+1];
NSLog(#"%#",butt.titleLabel.text);
}
it prints null not give button title............
Note:i am using button.currentitle also
how is it possible..why its displays null?
Since you set the title with setTitle:forState:, use titleForState: to get the title:
NSLog(#"title is %#", [butt titleForState:UIControlStateNormal]);
Of course this assumes that butt isn't nil. You need to assign the button's tag after you allocate the button:
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag=i+1;
Make sure you define your button in for loop like
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
and than specify tag.
button.tag=i+1;
You have to define button in for loop instead of .h file.
And for get title text
for (int j=0;j<=[_englishArray count]; j++) {
UIButton *butt = (UIButton *)[_keyboardImageView viewWithTag:j+1];
NSLog(#"%#",butt.titleLabel.text);
}
hope it will help you.
_englishArray = [[NSMutableArray alloc]initWithObjects:#"A",#"B",#"C",#"D",#"E",#"F",#"G",#"H",#"I",#"J",#"K",#"L",#"M",#"N",#"O",#"P",#"Q",#"R",#"S",#"T",#"U",#"V",#"W",#"X",#"Y",#"Z", nil];
int i=0;
float x=5,y=13;
//add array elements as button title
for (i=0; i< [_englishArray count]; i++) {
button.tag=i+1;
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
button.frame= CGRectMake(x, y, 29, 30);
[button setBackgroundColor:[UIColor grayColor]];
[button setTitle:[_englishArray objectAtIndex:i] forState:UIControlStateNormal];
//button.titleLabel.textColor =[UIColor whiteColor];
[_keyboardImageView addSubview:button];
x= x+31;
if (x==320 || x>310) {
x=5;
y=y+40;
}
}
//here get the button title useing for loop
// and i add the each button to _keyboardImageView and set title as _english arry elements ...and the get buttons curren title for this code....
for (int j=0;j<=[_englishArray count]; j++) {
butt = (UIButton *)[_keyboardImageView viewWithTag:j+1];
NSLog(#"Required Result is %#",butt.titleLabel.text);
}
Try it ,it will work...

Adding an array of buttons to a particular subview

I have a subview that is hidden until a user clicks a button (The subview is a UIScrollview). I'd like to programmatically add a grid of buttons in that view. I'm not sure how to specify which view the buttons get added to. I'm trying to follow http://www.raywenderlich.com/130/how-to-write-a-custom-image-picker-like-uiimagepicker
the scrollview is called graphicView
This is what I have
for(int i=0;i<64;i++)
{
UIImage *buttonImage=[UIImage imageNamed:#"image.png"];
[arrayOfThumbnails addObject:buttonImage];
}
for(int i = 0; i < arrayOfThumbnails.count; ++i) {
UIImage *thumb = [arrayOfThumbnails objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*100+24, row*80+10, 64, 64);
[button setImage:thumb forState:UIControlStateNormal];
[button addTarget:graphicView
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[graphicView addSubview:button];
if (column == 2) {
column = 0;
row++;
} else {
column++;
}
}
This is the only part I'm not using from the tutorial....
[view setContentSize:CGSizeMake(320, (row+1) * 80 + 10)]; self.view
= view;
Make sure the UIImage is valid by checking the return value from [UIImage imageNamed:#"image.png"]
Check that your NSMutableArray has been initialized
Your for loop looks unusual, try changing to fast enumeration
for (UIImage *thumb in arrayOfThumbnails) {
// UIImage *thumb = [arrayOfThumbnails objectAtIndex:i]; <-- Not needed
...
}
Is graphicView being added as a subview somewhere? Set graphicView.backgroundColor = [UIColor redColor] so you can see at least it's being added.

Resources