selector keyword - objective-c

-(void)clicketbutton{
UIButton *mybutton = [UIButton buttonWithType:UIButtonTypeCustom];
[mybutton setTitle:#"Click here" forState:UIControlStateNormal];
[mybutton addTarget:self action:#selector(displayvalue:)forControlEvents:UIControlEventTouchUpInside];
}
-(void)displayvalue:(id)sender{
UIButton *resultebutton= [UIButton buttonWithType:UIButtonTypeCustom];
resultebutton=sender;// pls clear here.. my question here , it it possible or not. if possible how ? NSLog(#" The buttontitile is %# ", [resultebutton.Title] // here also. }
In the above code I create a button and set title as Click here. When I press that button, I want to print Click here, I mean its title. For that my code is here.

iid is the sender, a pointer to the control that's calling your displayvalue method. You're casting the pointer to an integer, and printing the integer result. I'm not sure exactly what you're trying to accomplish, but the fastest way to get an integer from a button to the button's action method is to store it in the tag property.
If you go into a little more detail on what you're working on, I might be able to describe the best way to model that in a Cocoa app. Also, a tip-- be sure to fix any warnings in your code before trying to figure out why something's not working! That id -> int assignment would have made the compiler complain, for example.

Related

How to add an action to a UIButton

I've been searching online for this answer, and every single post skips over the part of where to actually write the code for an action. I have a simple Interactive UIButton. And If i could just see a template of code that says "\write code for action here", that would be super helpful!!! ( it's for iPad IOS7 )
This is as far as I can get...
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
I think I understand that this is how to set up a potential action, but where do I write the actual code for the action itself?
I want to kind of expand more on what was answered here already, Both responses are correct but i want to explain why/how this all works.
[button addTarget:self
action:#selector(aMethod:)forControlEvents:UIControlEventTouchDown];
The first thing to look at; The target.
The target is the instance of a class, any class. The only requirement for this class is that it has to implement the action.
action is the method you wish to invoke when the user presses the button.
#selector(aMethod:) Basically think of this as a method signature. Because Objective-c is a dynamic language, aMethod: does not need to exist, but will crash your program if it does not.
So if we put this all together, Whenever I want to press this button:
The system will invoke the action method, on the target instance.
and for the method itself, it can look like this
- (void) aMethod:(id)sender { }
You would put the action related code in a method, in that class, named aMethod:
- (void)aMethod:(id)sender {
// your code for the action goes here
}
You perhaps might also want to use UIControlEventTouchUpInside for the control event.
Since you've set the target as self, the aMethod: method should be added to the same class.
- (IBAction)aMethod:(id)sender
{
// do something here
}

Trying to add UIButton on top of vrf-reader ContentView

I am trying to insert some buttons in the PDF page rendered by the open source iOs reader vfr-reader https://github.com/vfr/Reader
Basically this is the code I am trying to implement in the initWithFrame method of ReaderContentView:
if (theContentView != nil) // Must have a valid and initialized content view
{
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setFrame:CGRectMake(269, 262, 100, 100)];
[myButton addTarget:self action:#selector(links) forControlEvents:UIControlEventTouchUpInside];
[myButton setTitle:#"asdasd" forState:UIControlStateNormal];
[theContentView addSubview:myButton];
The button doesn't work, and I can see that it is rendered and flattened in some way so it doesn't seem to be a button anymore as you can see here at full zoom.
However running DCInspector, despite warning that
2013-05-24 12:27:34.560 Reader[45333:c07] DCIntrospect: *** WARNING: One or more values of this view's frame are non-integer values. This view will likely look blurry. ***
seems to identify the button properly when inspected.
To test this case, just download the reader project and put my code in ReaderContentView at line 113. I am pretty sure I am doing something totally wrong. Thank you for help!
it was a bit tricky but I discovered that userInteractionEnabled was not set properly on the container view. I should have supposed that :)

Best way to create dynamic list of links/buttons in iOS5 view controller

I want to create a dynamic set of links/buttons in an iOS5 view controller and am trying to figure out what might be the best way to do this.
For eg:
Item 1
Item 2
Item 3
:
:
Item N
Each of the Items is a link/button that is clickable and will do some action, like load another screen etc based on the link.
I don't know ahead of time how many items there might be so if all the items don't fit on the screen, I need to be able to scroll to view.
My question:
1. What is a better way of doing this? I could just create the labels and buttons dynamically but this seems rather cumbersome and I'm not entirely sure how I would differentiate between the different buttons (essentially I'd need some index to find out which Item was clicked).
2. Alternatively, I was wondering if I can just render this page as HTML and just have links? I've never done this and not sure how I'd associate a button with a link.
Any suggestions?
AK
You can try to use the tag property to store the index value you need when you create the button. Then evaluate it in the button tap handler by accessing using button.tag.
Maybe you can try Cordova for an HTML based approach. I'm not too familiar with it though, so I can't say for sure.
Hope it helps.
(1) You can assign UIButton tag property based on the button index. If any events were to trigger, you could recognize which button the event belongs to by checking the tag.
Sample :
// Initializing some buttons
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button1.tag = 1;
[button1 addTarget:self
action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2.tag = 2;
[button2 addTarget:self
action:#selector(buttonPushed:)
forControlEvents:UIControlEventTouchUpInside];
Selector for button events :
- (void)buttonPushed:(id)sender {
...
if ([sender tag] == 1) {
// do something after button1 event
} else if () {
// do something after button2 event
}
...
}
(2) If you choose to do it in HTML, you could check out CMHTMLView

objective-c "add buttons automatically"

i need any kind of idea.
this is the problem. i am parsing a xml file which contains the url of an image, name of buttons, url for link to another UIwebview or view, etc... those are the important. what i need is; if a read a name of the button, on the screen must to add a new button automatically with its own image and link. i mean if in the xml i have 6 tags with the information mentioned previously, in the screen must to have 6 buttons with image and link. if in the xml exists more they must exist in the screen or uiwebview too.
i appreciate your help or ideas!!
Well, you can begin by using the NSXMLParser to parse the XML and obtain the properties for your buttons.
NSXMLParser Class Reference
Once you know how many tags you need, you can iterate:
for (int i = 0; i < numTags; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
// The tag is so when clicked, you can determine which one was pressed
// It would help to have your links stored in an NSArray, so you can pull
// them out by index.
button.tag = i;
[button setTitle:buttonName forState:UIControlStateNormal];
[button addTarget:self action:#selector(openButtonLink:) forControlEvents:UIControlEventTouchDown];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = ...; // How do you want your buttons laid out?
[someView addSubview:button];
}
I would also recommend checking out the class reference for UIButton:
UIButton Class Reference

Get UIButton from view programmatically

Im creating and adding a grid of buttons to my custom view keyboardView as follows:
int offset = 0;
for (int row = 0; row<4; row++){
for (int col = 0; col<13;col++) {
offset +=1;
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
aButton.frame = CGRectMake(5+col*65+offset,5+row*65, 60, 60);
[aButton setTitle:myarray[row][col] forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[keyboardView addSubview: aButton];
}
}
I need certain buttons to be of different sizes, like the return key or space bar. How can i get a reference to a particular button programmatically, later on in the same method? Is there an easier way than setting the tag and then calling [keyboardView viewWithTag:t]? Becauseint's are going to get confusing.
Thanks.
You could make instance variables like UIButton *spaceBar. if you reach the Button in the two for-Iretations which is thought to be the spacebar just do spacebar = aButton.
So you can later in the method just use this instance Variable which refers to the specified button. ;-)
I hope it's more or less understandable. ^^
You can either do it with UIView tags (which don't have to get confusing, just create an enum), or if you have only a few "special" UIButtons, you can create ivars to keep references to them.