I am learning Xcode(objective C). I want to make simple calculator.
I started by adding four simple buttons and one label. Two buttons are for numbers(1 and 2), and I added variables into .m file:
int *one = 1;
int *two = 2;
Next step what I've done is that I made action on button clicked and said that label take value from that button:
self.firstLabel.text=[NSString stringWithFormat:#"%d", one);
And I made same action for another button.
Everything is fine until this step, what I want to do next is when I click on number button to add value to that label, then when I click to divide(/) button to add / sign and when I click to another button to add another value.
I want to be something like this in label: 2 / 1, and when i click = button to divide them and show result. Thanks.
EDIT:
I also added label proper into .h, and when I click on button it shows me only one value in label.
Since you are just starting to learn obj C you may want to keep your calculator simple, building a fully functional calc. can get surprisingly complex. You should consider using an RPN approach, it will make things much easier, you can just push the numbers to a stack and perform operations one at a time.
Something like: 2, enter, 1, enter + enter.
You may also want to have a look at Stanford's iOS course on Apple University, the course is in Swift but the class project was a calculator so it should give you a good reference point. Hope that helps and good luck!
you should add a custom target for each button, i.e.,
- (IBAction)addDivide:(id)sender {
self.firstLabel.text = [[NSString alloc] initWithFormat:#"%# /", self.firstLabel.text];
}
Any action will update your label.
Just a further advice for you, don't name the label as "firstLabel", try to give it a name tied to its semantics.
You are only setting the int value to the label. You need to append the value maintaining the previous text so that it appears as "2 / 1".
I'll suggest using tags for the buttons and use a common method for number buttons in your calculator.
This is more sort of logical thing rather than specific to iOS and Xcode.
Similarly when "=" is pressed use a method to calculate the result.
Related
Scenario: User has 5 stars (buttons). Each button has a UIAccessibility label to describe the star ("First Star of Five", etc.).
Problem: Upon selection, UIAccessibility repeats the label. I want to change that to 'Selected' (or "You have selected the 2nd Star.").
Attempted Solution:
I tried to squash the button's action response and got silence; which is expected, I guess.
cell.button2.accessibilityLabel = #"Second star of Five.";
cell.button2.accessibilityTraits += UIAccessibilityTraitStartsMediaSession;
But I also don't want an automated repeat of the label.
How can I have full control of 'Voice Over' upon selection of each star button so user understands the selection vs merely repeating the label?
Per suggestion below, I can't override the accessibilityLabel of my custom button class. I have ObjC code, but prefer to work with Swift. So I'm trying to incorporate the custom UIButton class written in Swift, into ObjC.
Define a CustomStarButton and overwrite the - (NSString *)accessibilityLabel{} func, if selected, return #"You have selected the 2nd Star."
I am trying a lot of code and it's nothing is working for me. I need to make four radio buttons with swift when I show the question for the user to choose one of radio buttons.
Sadly, there's not a built in way to do this with Swift, but it's easy enough to code out. I'd personally recommend using this Git project, as I've heard good things about it.
If you'd like to built a radio button from scratch, I'd suggest looking a making a check button by using a regular button with a UIControlStateSelected triggering a check img, and then set the layer.cornerRadius (after setting the clipsToBounds = true of course) of your button to half its width to produce a circle!
You can create your own login by using a mutabledictionary with button tag as key for dictionary. Add green/selected image for selcted button & set "yes" value for key (button tag) in dictionary for selected button & "no" value for other button's tag.
I have an example for the same but in Objective -C
The UIControl that offers the closest behavior from radio button in swift iOS is the UISegmentedControl, take a look at the reference.
Here's apple's guidelines for mobile controls.
Some quotes from this article :
Use a segmented control to offer choices that are closely related but mutually exclusive.
A segmented control:
Consists of two or more segments whose widths are proportional, based on the total number of segments
Can display text or images
My app has a text field that accepts a number. After the user fills the number, I want my user to have two choice of return buttons. One return to run function A, and one return to run function B. They also have to hold down the return button for 2 seconds to make it work.
Does anyone know how to do this? Please answer in Swift.
Configure and add buttons to the text field's inputAccessoryView; this view, including its subviews (the buttons), will appear at the top of the keyboard.
Before I start I want to explain that I'm looking for a general solution to my inquiry, I don't have any code to show because I haven't the faintest idea of how to start. Let's say I have a program in which I have 4 buttons, two on the right (red and blue), two on the left (white and yellow). If I were to press a certain combination of buttons I want to get a specific output an example would be if I touch the red button and then the white one the word "cow" appears or if were to touch the white one then the red one the word "chicken" appears. Is there any documentation that anyone knows of that can help me with my problem? Can this even be done with an array?
HotLicks has a valid answer, but I think an NSDictionary is a better solution. Create keys based on button presses, like:
#{ #"RedWhite" : #"Cow",
#"WhiteRed" : #"Chicken" };
Your code will be easier to read, and you can support three presses easily (assuming you're working with a timer that you invalidate after each press) by just adding another key; no recalculating your array:
#{ #"RedWhiteBlue" : #"America" };
Is it possible to add a text link into a TextView? I want the link to perhaps behave like a button, where I can assign an action to it.
EDIT: When I say assign an action, I mean actually giving it something in the code. I'm wondering if it's possible to dynamically add a "button" into text that I can assign a coded action to.
Live scenario
Think of something like a dictionary app. Maybe the definition of one word uses another word that you might not know the definition of, so being able to click on that word to instantly search it rather than having to type it in would be a nice user friendly feature. It seems rather unlikely, though, I guess.
I would recommend using NIAttributedLabel from Nimbus, an open source iOS library. You can specify text ranges that are links, and you get delegate messages sent when a user taps on it.
Main Nimbus site: http://nimbuskit.info/
NIAttributedLabel docs: http://docs.nimbuskit.info/interface_n_i_attributed_label.html
in the inspector, go to the Text View Attributes tab then make sure "Detect Links" is checked.
Yes you can. Add the URL into the text view, then open up the Attributes Inspector. You will see an option in there to detect links.
I know of a way, but its a LOT of work. First, you have an NSAttributedString that you have the text view display. Second, attribute the range of text you want to be the button. Third, assign a tap gesture recognizer to the text view and in the method called by the recognizer, you'll use core text to determine if the tap happened over the range of text that represents the buttons.
Heres how youll use core text: create a framesetter with the attributed string. Create a frame from the framsetter with the shape of a square that is the frame of the text view, inset by the padding of the text view. The frame will allow you to get the y origins of every line in the text view and and once you know what line the tap happened on, you can use the line to then figure out exactly what character was tapped on that line by giving it an x offset. Once you know character index on the line, you can add it to the beginning of the range of the line and get the index of the character within the whole string. Then you can check if its within the range of the text that is your button. If it is, you can then call a method to simulate a target action type behavior.
Ive explained the process of how to accomplish this and specified what kinds of core text objects youll need, ill let you look up the specific api details:
https://developer.apple.com/library/mac/documentation/Carbon/Reference/CoreText_Framework_Ref/_index.html#//apple_ref/doc/uid/TP40005304
You can also use my objc core text wrapper:
https://github.com/mysterioustrousers/MYSCoreText
What about CoreText? It Can draw many kinds of Text .