Setting Property on UIButton in Loop - iphone

I'm spinning in circles on the best way to implement this, so maybe you can help me out:
How can I make it so when the UIButton responds to the selector highlightImage, it also can set the desired challengeId = the index of my appDelegate.availableArray?
In my cellForRowAtIndexPath method, I'm creating a UIScrollView with buttons:
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, tv.frame.size.width, 78)];
[scrollView setContentSize:CGSizeMake(500, 78)];
[self createScrollButtons];
[[cell contentView] addSubview:scrollView];
My createScrollButtons loops and creates buttons in the scrollView as so, looping over the number of elements in my availableArray
- (void) createScrollButtons
{
SuperHeroGameAppDelegate *appDelegate = (SuperHeroGameAppDelegate *)[[UIApplication sharedApplication] delegate];
int x = 0;
int y = 0;
NSLog(#"Challenge array = %#",appDelegate.availableArray);
for (int i = 0; i < [appDelegate.availableArray count]; i++) {
NSLog(#" ID= %d", [[[appDelegate.availableArray objectAtIndex:i]valueForKey:#"id"] intValue]);
[self createButtonAtX:x AndY:y withChallenge:[[appDelegate.availableArray objectAtIndex:i]valueForKey:#"id"]];
x += 90;
}
}
createButtonAtX andY withChallenge is called for each element inavailableArray`, positioning them correctly along the scrollView.
- (void)createButtonAtX:(int) x AndY:(int) y withChallenge:(id)challengeId
{
CGRect buttonRect = CGRectMake(x, y, 80, 78);
ChallengeButton *capeButton = [ChallengeButton buttonWithType:UIButtonTypeCustom];
[capeButton setBackgroundColor:[UIColor clearColor]];
[capeButton setFrame:buttonRect];
[capeButton addTarget:self action:#selector(highlightImage:) forControlEvents:UIControlEventTouchUpInside];
UIImage *capeImage = [UIImage imageNamed:#"CapePower.png"];
[capeButton setBackgroundImage:capeImage forState:UIControlStateNormal];
[scrollView addSubview: capeButton];
}
highlightImage is called as each buttons selector, to determine which button is pressed. (and ideally with element from the availableArray that button is associated with)... I've subclasses UIButton and added an int value to be able to call [sender setChallengeId:]
- (void) highlightImage:(id)sender
{
if([sender isSelected] == NO){
[sender setSelected:YES];
[sender setChallengeId:selectedChallenge];
NSLog(#"sender = %d",[sender challengeId]);
UIImage *selectedImage = [UIImage imageNamed:#"PowerBarHand.png"];
[sender setBackgroundImage:selectedImage forState:UIControlStateSelected];
}
else {
[sender setBackgroundImage:[UIImage imageNamed:#"HandstandPower.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
selectedChallenge = 0;
}
}
My problem is I cannot figure out how to get the appDelegate's availableArray element INTO my highlightImage, as UIButton's [capeButton addTarget:challengeId action:#selector(highlightImage:) forControlEvents:UIControlEventTouchUpInside] will not allow me to pass the challengeId from the loop earlier in the flow.
I am able to pass any integer value to setChallengeId, and it is handling as expected. I just need to get the appDelegate.availableArray id value from the loop.
Any direction would be great!:)

You get availableArray into highlightImage: the same way you got it into createScrollButtons:
SuperHeroGameAppDelegate *appDelegate = (SuperHeroGameAppDelegate *)[[UIApplication sharedApplication] delegate];
and then access appDelegate.availableArray.
I think that's not the question you actually meant to ask, though. I think what you want is to call setChallengeId: on each ChallengeButton as you create it in createButtonAtX:AndY:withChallenge:. Then in highlightImage: do selectedChallenge = [sender challengeId] rather than [sender setChallengeId:selectedChallenge].

Related

Change frame of cancel button in UISearchBar

I have the following code to change the appearance of the UISearchBar in my application and it can be seen in the image below also:
for(int i = 0; i < [[searchBar subviews] count]; i++){
UIView *subView = [[searchBar subviews] objectAtIndex:i];
if([[NSString stringWithFormat:#"%#", [subView class]] isEqualToString:#"UINavigationButton"]){
UIButton *cancelButton = (UIButton *)subView;
CGRect buttonFrame = cancelButton.frame;
buttonFrame.size.height = 52;
[cancelButton setFrame:buttonFrame];
[cancelButton setTitle:#"Cancel" forState:UIControlStateNormal];
[cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel.png"] forState:UIControlStateNormal];
[cancelButton setBackgroundImage:[UIImage imageNamed:#"cancel_pressed.png"] forState:UIControlStateHighlighted];
}
}
As you can see I am trying to change the height of the button contained in the UISearchBar with no luck. I do get reference to the button as I can change the text and the background Image is changed just not the height of the frame. I would just like the height of the button to be the same as the search box at 52px.
EDIT:
I have found a really hacky solution, but it's not very elegant of adding a UIButton as the subview of the Cancel Button. It does the job but as I say it's not very nice.
Actually you could use method below to access the cancel Button:
UIBarButtonItem *cancelButton = [UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil];
So I override -layoutSubViews with this, but theres 2 issues I have
I hate iterating through the views to find the matching view, and it messes up the animation a little bit
- (UIButton *)cancelButton {
for (UIView *v in self.subviews) {
if ([v isKindOfClass:[UIButton class]]) {
NSString *caption = [[((UIButton *)v) titleLabel] text];
if ([caption isEqualToString:#"Cancel"] )
return (UIButton *)v;
}
}
return nil;
}
- (BOOL)cancelButtonIsShowing {
return ([self cancelButton] != nil);
}
- (void)layoutSubviews {
[super layoutSubviews];
if ([self cancelButtonIsShowing]) {
UIButton *cancelButton = [self cancelButton];
UIImage *i = [UIImage imageNamed:#"Cancel_BTN"];
CGRect f = CGRectMake(267, 5, i.size.width, i.size.height);
cancelButton.frame = f;
}
}

clear uibutton title and reset again

I Follow the comment and try to fix my problem,but still not working.
when i run the test demo on the simulator,i get this:
run the demo project view
and i click the test2, i want to change button title before clear the button title, but i
get this :
after i click test2 button
i can not clear the button title when click another button.
anyone can help ??
Here is my code
-(void)addbutton
{
for (int i=0; i<3; i++)
{
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake((i*100), 0, 100, 100)];
button.titleLabel.text = #"";
[button setTag:i];
[button setTitle:[self.demoDataArray objectAtIndex:i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
[button addTarget:self action:#selector(setButtonTitle:) forControlEvents:UIControlEventTouchUpInside];
[self.demoView addSubview:button];
}
}
-(IBAction)setButtonTitle:(id)sender
{
if ([sender tag] == 0)
{
self.demoDataArray = [[NSArray alloc] initWithObjects:#"test5", #"test6", #"test7", #"test8", nil];
[self addbutton];
}
else if([sender tag] == 1)
{
self.demoDataArray = [[NSArray alloc] initWithObjects:#"test9", #"test10", #"test11", #"test12", nil];
[self addbutton];
}
else if([sender tag] == 3)
{
self.demoDataArray = [[NSArray alloc]initWithObjects:#"test13", #"test14", #"test15", #"test16", nil];
[self addbutton];
}
}
Every time any button is pressed, you instantiate new buttons and add them on top of the other ones. You probably want to just update the existing buttons. Try this for your -addButton method instead:
-(void)addbutton
{
for (int i=0;i<3;i++)
{
NSInteger tag = i+1;
UIButton *button = (UIButton *)[self.view viewWithTag:tag];
if (!button)
{
button = [[UIButton alloc] initWithFrame:CGRectMake((i*100), 0, 100, 100)];
[button addTarget:self action:#selector(setButtonTitle:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:tag];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
[self.demoView addSubview:button];
}
button.titleLabel.text = #""; // Not actually sure you need this...
NSString *titleText = nil;
if (i < self.demoDataArray.count)
titleText = [self.demoDataArray objectAtIndex:i];
[button setTitle:titleText forState:UIControlStateNormal];
}
}
Now the button is instantiated, given a target and action, tagged, and added into the button hierarchy only when it doesn't already exist. Every time a button is tapped after that, only the titles will be updated.
Also, very important:: in order for -viewWithTag: to work, you have to use a tag for your buttons that is not equal to 0, the default tag - otherwise the button's superview will be returned. This means you'll need to make the following changes in your button handler, which already had a bug with checking the tags (checking against 3 rather than 2). Increment the tags to start at 1 and end with 3 like so:
-(IBAction)setButtonTitle:(id)sender
{
if ([sender tag] == 1)
{
self.demoDataArray = [[NSArray alloc] initWithObjects:#"test5",#"test6",#"test7",#"test8", nil];
[self addbutton];
}
else if([sender tag] == 2)
{
self.demoDataArray = [[NSArray alloc] initWithObjects:#"test9",#"test10",#"test11",#"test12", nil];
[self addbutton];
}
else if([sender tag] == 3)
{
self.demoDataArray = [[NSArray alloc]initWithObjects:#"test13",#"test14",#"test15",#"test16", nil];
[self addbutton];
}
}
That method could use some cleanup as well to eliminate duplicated code and it's probably more appropriate to use a switch statement here anyways. But that's neither here nor there.
OK, this should probably get you up and running. I didn't test or compile this so if you do have some problem with the compiler that you're not able to work through on your own please let me know and I'll see if I can spot the issue.
try this
- (IBAction)firstbtn:(UIButton *)sender {
[secondbtn setTitle:#"" forState:UIControlStateNormal];
}

How to create UISwitch in for loop in iphone?

I have done following code in Viewdid Load for creating multiple switch programmatically.
float x =40.0, y=20.0,height=60.0,width=26.0;
for (int i =0; i < 3; i++) {
CGRect frame = CGRectMake(x, y, height, width);
UISwitch *switchControl = [[UISwitch alloc] initWithFrame:frame];
[switchControl addTarget:self action:#selector(flip:) forControlEvents:UIControlEventTouchUpInside];
[switchControl setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:switchControl];
y= y+50.0;
}
- (IBAction) flip: (id) sender {
UISwitch *onoff = (UISwitch *) sender;
NSLog(#"%#", onoff.on ? #"On" : #"Off");
}
After doing this code i am able to create multiple UISwitchs. Now i am not getting how to handle action on every switch. If anyone knows how to handle actions on all switchs please help me. I will appreciate to him/her. Thanks in advance.
for (int i =0; i < 3; i++) {
CGRect frame = CGRectMake(x, y, height, width);
UISwitch *switchControl = [[UISwitch alloc] initWithFrame:frame];
//add tag as index
switchControl.tag = i;
[switchControl addTarget:self action:#selector(flip:) forControlEvents: UIControlEventValueChanged];
[switchControl setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:switchControl];
y= y+50.0;
}
- (IBAction) flip: (id) sender {
UISwitch *onoff = (UISwitch *) sender;
NSLog(#"no.%d %#",onoff.tag, onoff.on ? #"On" : #"Off");
//use onoff.tag , you know which switch you got
}
it is UIControlEventValueChanged event
[switchControl addTarget:self action:#selector(flip:) forControlEvents:UIControlEventValueChanged];

Is it possible to scroll from UIWebView object to a UIView object?

In one of my app's user interfaces i want my app display a scrolling view(the count of the scrolled pages is dynamically set in runtime). But i want the to add UIWebView and UIView elements to the same array, so that when i scroll i will be able to display both UIWebView(let's say a web page) and some other View?
EDIT
I received this answer which looks as if exactly ok but doesn't work(or i couldn't to)
here is the code:
NSArray *myArray;
// Fill the array with a bunch of UIWebView and UIView objects here
for (id theObject in myArray)
{
if ([theObject isKindOfClass:[UIWebView class]]) {
// do stuff
}
else if ([theObject isKindOfClass:[UIView class]]) {
// do other stuff
}
}
Can anyone please give a hend of help? I need to create a scrollable array of views(like the main page of the iPhone) but the first page(mean, view) must be a member of UIWebView(the others are UIView)
And below is all my code(i han't try to do anything here:)
CGRect frame;
[webView initWithFrame:frame];
NSArray *colors = [NSArray arrayWithObjects:webView, [UIColor blackColor],[UIColor blackColor], nil];
for (int i = 0; i < colors.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
UIView *subview = [[UIView alloc] initWithFrame:frame];
subview.backgroundColor = [colors objectAtIndex:i];
[subview setTag:i];
if([subview tag]==2){
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(70.0f, 600.0f, 250.0f, 60);
[button setTitle:#"Read Messages" forState:UIControlStateNormal];
for(NSDictionary* buttonData in butDat) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
NSString* buttonTitle = [buttonData objectForKey:#"name"];
NSString* buttonID=[buttonData objectForKey:#"order_number"];
[button setTitle:buttonTitle forState:UIControlStateNormal];
[button setTag:[buttonID intValue]];
button.titleLabel.font=[UIFont systemFontOfSize:28];
CGFloat yPosition = 60.0f;
const CGFloat buttonHeight = 85.0f;
if (button.tag==1) {
button.frame = CGRectMake(70.0f, yPosition, 250.0f, buttonHeight);
//[ma.view addSubview:button];
}
if (button.tag==2) {
button.frame = CGRectMake(440.0f, yPosition, 250.0f, buttonHeight);
//[ma.view addSubview:button];
}
if (button.tag==3) {
button.frame = CGRectMake(70.0f, 200.0f, 250.0f, buttonHeight);
//[ma.view addSubview:button];
}
if (button.tag==4) {
button.frame = CGRectMake(440.0f, 200.0f, 250.0f, buttonHeight);
//[menu_2.view addSubview:button];//****
}
if (button.tag==5) {
button.frame = CGRectMake(70.0f, 335.0f, 250.0f, buttonHeight);
//[menu_2.view addSubview:button];//****
}
if (button.tag==6) {
button.frame = CGRectMake(440.0f, 335.0f, 250.0f, buttonHeight);
//[menu_2.view addSubview:button];//****
}
[button addTarget:self action:#selector(secondAct:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
}
//
[button addTarget:self action:#selector(readM:) forControlEvents:UIControlEventTouchUpInside];
// [scrollView addSubview:button];
[self.view addSubview:button];
// [self.scrollView addSubview:button];
}
[self.scrollView addSubview:subview];
There's nothing stopping you from adding objects of type UIWebView and UIView to the same NSArray.
If your code needs to know if you're dealing with a UIWebView or a UIView, you can use the method isKindOfClass: to check:
NSArray *myArray;
// Fill the array with a bunch of UIWebView and UIView objects here
for (id theObject in myArray)
{
if ([theObject isKindOfClass:[UIWebView class]]) {
// do stuff
}
else if ([theObject isKindOfClass:[UIView class]]) {
// do other stuff
}
}
Furthermore, UIWebView is a subclasses of UIView and inherits many of the same methods, making view layout the same in both cases.
From a user interface standpoint, it's probably bad practice to have scroll views inside scroll views, so you're going to want to disable scrolling of your UIWebView objects. If you are targeting iOS 5 and above you can use the following:
webView.scrollView.scrollEnabled = NO;
webView.scrollView.bounces = NO;
and if you're on a version of iOS lower than 5.0, you can inject the following javascript:
<script type="text/javascript">
touchMove = function(event) {
event.preventDefault();
}
</script>
To implement the javascript injection, you can use UIWebView's stringByEvaluatingJavaScriptFromString: method inside webViewDidFinishLoad:
See here for more information on UIWebView javascript injection: http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview
Edit: Looking at your code, it seems as thought you're sticking a UIWebView and two UIColor objects into your array. Therefore you need to check if you're dealing with a UIWebView or a UIColor, or you'll crash on subview.backgroundColor = [colors objectAtIndex:i]; Also you set your UIWebView object's frame to CGRect frame; which is uninitialized.
You should populate your NSArray with your UIWebView and then two UIView objects with their background colors set:
UIWebView *webView = [[UIWebView alloc] init];
UIView *viewOne = [[UIView alloc] init];
[viewOne setBackgroundColor:[UIColor blackColor]];
UIView *viewTwo = [[UIView alloc] init];
[viewTwo setBackgroundColor:[UIColor blackColor]];
NSArray *viewArray = [NSArray arrayWithObjects:webView, viewOne, viewTwo, nil];
for (id theObject in viewArray)
{
if ([theObject isKindOfClass:[UIWebView class]]) {
// do stuff
}
else if ([theObject isKindOfClass:[UIView class]]) {
// do other stuff
}
}
Yes. UIWebViews descend from UIViews. You may use them wherever you would insert a UIView as a subview, including within a UIScrollView parent view, or as an object to be inserted into an array.
Your scroll view is not scrolling because you didn't set the contentsize property. First you have to find the content size appropriately, and then you have to set content size as
[scrollView setContentSize:CGSize];
at the end of code. ie; after
[self.scrollView addSubview:subview];
For a blind try, use
[scrollView setContentSize:CGSizeMake(1000,1000)];
this will definitely scroll the view!!!

UIButtons in UIScrollView stop it scrolling, how to fix?

I have a problem with adding a set of UIButtons to a UIScrollView. With the buttons added it seems that the scroll command is not being passed to the scroll view as the buttons cover the whole surface. I've read various posts on this but still can't figure it out. I'm pretty new to iPhone programming so there's probably something obvious I've missed. I've tried things like canCancelContentTouches and delaysContentTouches to TRUE and FALSE.
Here is the code I use to add the buttons to the UIScrollView. The UIScrollView was created in IB and is passed in to the function:
-(void)drawCharacters:(NSMutableArray *)chars:(UIScrollView *)target_view {
//NSLog(#"CLEARING PREVIOUS BUTTONS");
//clear previous buttons for parts/kanji
if(target_view == viewParts){
for (UIButton *btn in parts_buttons) {
[btn removeFromSuperview];
}
[parts_buttons removeAllObjects];
} else if(target_view == viewKanji){
for (UIButton *btn in kanji_buttons) {
[btn removeFromSuperview];
}
[kanji_buttons removeAllObjects];
}
//display options
int chars_per_line = 9; //change this to change the number of chars displayed on each line
if(target_view == viewKanji){
chars_per_line = 8;
}
int char_gap = 0; //change this to change the margin between chars
int char_dimensions = (320/chars_per_line)-(char_gap*2);
//set starting x, y coords
int x = 0, y = 0;
//increment y coord
y += char_gap;
//NSLog(#"ABOUT TO DRAW FIRST BUTTON");
for(NSMutableArray *char_arr in chars){
//increment x coord
x += char_gap;
//draw at x and y
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(x, y, char_dimensions, char_dimensions); // position in the parent view and set the size of the button
[myButton setTitle:[char_arr objectAtIndex:0] forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1.0] forState:UIControlStateDisabled];
myButton.titleLabel.font = [UIFont boldSystemFontOfSize:22];
if(target_view == viewKanji){
myButton.titleLabel.font = [UIFont boldSystemFontOfSize:30];
}
// add targets and actions
if(target_view == viewParts){
[myButton addTarget:self action:#selector(partSelected:) forControlEvents:UIControlEventTouchUpInside];
} else if(target_view == viewKanji){
[myButton addTarget:self action:#selector(kanjiSelected:) forControlEvents:UIControlEventTouchUpInside];
[myButton setTag:(int)[char_arr objectAtIndex:1]];
}
//if the part isnt in the current list of kanji, disable and dim it
if(target_view == viewParts){
if([kanji count] > 0){
[myButton setEnabled:NO];
}
bool do_break = NO;
for(NSMutableArray *arr in kanji){
//NSLog([NSString stringWithFormat:#"CHECKING PARTS AGAINST %d KANJI", [kanji count]]);
for(NSString *str in [[arr objectAtIndex:2] componentsSeparatedByString:#" "]){
if(([myButton.titleLabel.text isEqualToString:str])){
//NSLog(#"--------------MATCH!!!-----------------");
[myButton setEnabled:YES];
for(NSString *str1 in parts_selected){
if(([myButton.titleLabel.text isEqualToString:str1])){
[myButton setEnabled:NO];
break;
}
}
do_break = YES;
break;
}
if(do_break) break;
}
if(do_break) break;
}
}
// add to a view
[target_view addSubview:myButton];
//update coords of next button
x += char_dimensions+ char_gap;
if (x > (320-char_dimensions)) {
x = 0;
y += char_dimensions + (char_gap*2);
}
//add button to global array to be removed from view next update
if(target_view == viewParts){
[parts_buttons addObject:myButton];
} else if(target_view == viewKanji){
[kanji_buttons addObject:myButton];
}
}
//NSLog(#"FINISHED DRAWING ALL BUTTONS");
}
Any help on this would be greatly appreciated. I just need to fix this to finish my app.
You cannot expect the scrollview to receive events if the buttons cover the whole surface.(They are going to be first in the responder chain). The quick fix would be to make the buttons slightly smaller than the scrollview.
On first read of this problem I was thinking that you wanted to add buttons to the scrollview's superview -- but then I realized you actually want the buttons in the scrollview.