Professional Documents
Culture Documents
IOS Exam Practical 2
IOS Exam Practical 2
2.
Now let's right-aligne labels.
○ Click the top label, hold down the Shift key, and click the second
label to select both.
○ If the Attributes Inspector is not visible, press
Option+Command+4 to bring it up, make sure the Label section
is expanded.
○ Use the Alignment control in the inspector to make the content of
these labels right-justified.
○ Make a constraint to make sure these two fields are always the same
width by selecting at the bottom of the screen Pin | Widths Equally.
○ Make sure that string width is equal to number width; if not reverse
items
○ If there are still some layout problems
○
you can fix tem on your own or use Resolve Auto Layout Issues | Add
Missing Constraints
○
and next Resolve Auto Layout Issues | Update Frames.
3. Step 4: inspecting text field attributes
Spend a few minutes inspecting text field attributes in the Attributes
inspector (Option+Command+4). Working with it
○ Type in the text Type in a string as the placeholder for string text field.
○ Change default value in the Return Key pop-up. The Return key is the
key on the lower right of the virtual keyboard, and its label changes based
on what we’re doing: it coud be Search for web browser, Next for a form or
Done for independent fields. To keep it different from default, let's change it
to Done.
○ Type in the text Type in a number as the placeholder for number
text field.
○ From the Keyboard pop-up menu select Number Pad (since we want
the user to enter only numbers, not letters).
○ For both text fields check the Opaque check box and uncheck Clears
Graphics Context and Clip Subviews.
By selecting Opaque, we inform the system that nothing behind a view ever
needs to be drawn, so it does not need to waste processing time.
When Clears Graphics Context is checked the system will draw the entire
area covered by the object in transparent black before it actually draws the
object. Again, for performance reasons it should be turned off.
Clip Subviews is an interesting option. If your view contains subviews, and
those subviews are not completely contained within the bounds of its parent
view, this check box determines how the subviews will be drawn. If Clip
Subviews is checked, only the portions of subviews that lie within the
bounds of the parent will be drawn. If Clip Subviews is unchecked, subviews
will be drawn completely, even if they lie outside the bounds of the parent.
Again, for performance reasons it should be unchecked because calculating
the clipping area and displaying only part of the subviews is a costly
operation while in most cases a subview won’t lie outside the bounds of its
superview so checking this field would result a CPU wast of time.
4. Step 5: add an outlets
At this point we should be familiar with outetd because we did them in the
previous part of this tutorial. Make sure ViewController.m is showing in the
Assistant Editor. As we did it before, Control-drag from the top text field in
the view over to the ViewController.m file, right below the @interface
line. We should see a gray pop-up that reads Insert Outlet, Action, or Outlet
Collection.
5.
Release the mouse button, type textFieldString into the Name field and
then accpt by hit Return or click the Connect button.
6.
Repeate the same process for the second text field, creating and
connecting it to a property called textFieldNumber.
7. Step 6: closing the keyboard for string field
When the user taps the Done button on the keyboard, a Did End On
Exit event will be generated. This is a right time to tell the text field to
give up control so that the keyboard will go away.
To bind some event with an action, we can proceed as for the Previous
button in previous part of this tutorial (see Step 3: designing the user
interface (add first button) at First application). On the other hand,
this is a good chance to show other method so let's do it.
Add the following action method at the bottom of ViewController.m
8.
- (IBAction)textFieldStringEditDone:(UITextField *)sender {
[sender resignFirstResponder];
}
9.
The first responder is the control with which the user is currently interacting.
In the above method we tell our control to resign as a first responder so to
the previous control the user worked with will take this role. When a text
field yields first responder status, the keyboard associated with it goes away.
Select Main.storyboard in the Project Navigator, single-click the top text
field, and press Option+Command+6 to bring up the connections inspector.
10.
Press Control button and drag from the circle next to Did End On Exit to the
yellow View Controller icon in the storyboard, in the bar that’s just above the
view you’ve been configuring, and let go.
11.
A small pop-up menu will appear containing the name of a single action, the
one we just added. Click the textFieldStringEditDone: action to select it.
12.
You can also do this by dragging to the textFieldStringEditDone: method
in the assistant view.
13.
Repeat this procedure with the second text field.
14. Step 7: closing the keyboard for number field
Unfortunately not all keyboard layouts feature a Return button. To
solve this problem we will implement most natural approach: if the
user tap anywhere in the view where there’s no active control the
keyboard will go away.
Add the following action method at the bottom of ViewController.m
15.
- (IBAction)tapOnBackground:(id)sender {
[self.textFieldString resignFirstResponder];
[self.textFieldNumber resignFirstResponder];
16.
Next we should change the underlying class of the view property inherited
by our view controller from UIViewController. Now this property points to
an instance of UIView that acts as a container for all the items in our user
interface. If we change the class of the object that view points to and set it
as UIControl, the view will behave like controls object (for example button).
It is safe to do this, because UIControl is a subclass of UIView.
Press Option+Command+3 to bring up the Identity Inspector.
17.
and change the class to UIControl. Next press Option+Command+6 to bring
up the Connections Inspector. As we did it for text fields, drag from the
Touch Down event to the View Controller icon, and choose the
tapOnBackground: action.
○ an outlet labelForSlider;
○ an outlet slider;
○ an action sliderChange.
Step 4: ensure correct initial values for slider and corresponding label
Change in ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.labelForSlider.text = @"00000000";
self.slider.value = 0;
- (IBAction)sliderChange:(UISlider *)sender {
- (NSString *)decimalToBinaryAsString:(int)decimalValue{
}
Segment Control
Step 1: add segmented control and label
Drag a Segmented Control and Labelfrom the Object Library and place
it on the view.
7.
Modify and add code to viewDidLoad function
8.
- (void)viewDidLoad {
[super viewDidLoad];
self.slider.value = 0;
[self.segmentedControl setSelectedSegmentIndex:0];
self.labelNumeralSystem.text = [self.segmentedControl
titleForSegmentAtIndex:self.segmentedControl.selectedSegmentIndex];
- (IBAction)switchLowerChange:(UISwitch *)sender {
- (IBAction)buttonSwitchResetPress:(UIButton *)sender {
[self switchResetToDefaut];
- (void)switchResetToDefaut{
}
for (UILabel *label in self.labelForSwitchCollection){
label.text = @"Off";
label.text = switchUI.isOn?@"On":@"Off";
}
- (void)viewDidLoad {
[super viewDidLoad];
self.slider.value = 0;
[self.segmentedControl setSelectedSegmentIndex:0];
self.labelNumeralSystem.text = [self.segmentedControl
titleForSegmentAtIndex:self.segmentedControl.selectedSegmentIndex];
[self switchResetToDefaut];