judaco / iPhone

0 stars 0 forks source link

iPhone - Class 07 #3

Open judaco opened 7 years ago

judaco commented 7 years ago

Segment Control (Array List)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    UISegmentedControl * segmentControl;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray<NSString*> * segments = [[NSArray alloc]initWithObjects:@"real estate",@"cars",@"yad2",@"jobs", nil];

    segmentControl = [[UISegmentedControl alloc] initWithItems:segments];
    //segmentControl.center = self.view.center;
    segmentControl.frame = CGRectMake(5, 30, self.view.frame.size.width - 20, 40);
    [self.view addSubview:segmentControl];

    [segmentControl addTarget:self action:NSSelectorFromString(@"segmentControlValueChanged:") forControlEvents:UIControlEventValueChanged];
    segmentControl.momentary = YES;

}

-(void)segmentControlValueChanged:(UISegmentedControl *) sender{
    if (sender == segmentControl){
    long int value = segmentControl.selectedSegmentIndex;
    NSLog(@"selected index %li", value);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
judaco commented 7 years ago

לעשות טקסט עם מספר עשרוני שהיוזר מקליד וזה מופיע בתיבת טקסט, הוא יכול להגדיר רק נקודה אחת, וכל דבר אחר כמו אות או סימבול או נקודה הוא לא טוב. התחלה עם 0 זה לא חוקי, תמיד מופיע מספר שיכול להיות גם 0.

UITextField (EditText)

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    UITextField * textField;
    UILabel * lblCounter;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.textAlignment = NSTextAlignmentCenter;
    textField.center = self.view.center;
    textField.text = @"initial text";
    //textField.placeholder = @"please insert a name";
    [self.view addSubview:textField];

    lblCounter = [[UILabel alloc] initWithFrame:CGRectMake(textField.frame.origin.x, textField.frame.origin.y - 50, textField.frame.size.width, 30)];
    [self.view addSubview:lblCounter];
    [self calculateLength:textField.text];
     textField.delegate = self;
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    //NSLog(@"%li %li %@", range.location, range.length, string);
    NSString * text = [textField.text stringByReplacingCharactersInRange:range withString:string];

    [self calculateLength:text];
    return YES;
}

//when i get a string from user, I need to get the numbers of chars
-(void)calculateLength:(NSString *) text{
    NSString * label;
    unsigned long int length = [text lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
    if (length == 1){
        label = @"character";
        //NSLog(@"%li charachter", length);
    }else{
        label = @"characters";
        //NSLog(@"%li charachters", length);
    }

    label = [[NSString alloc] initWithFormat:@"%li %@", length, label];
    lblCounter.text = label;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end