Hiển thị các bài đăng có nhãn Object c. Hiển thị tất cả bài đăng
Hiển thị các bài đăng có nhãn Object c. Hiển thị tất cả bài đăng

Thứ Tư, 24 tháng 5, 2017

Override init function ViewController

- (id)initWithRelayData:(SecurityRelayItem *)relayData {
    self = [super init];
    if(self) {
        _relayData = relayData;
    }
    return self;

}

Chủ Nhật, 26 tháng 3, 2017

Get size image max scale with a frame

***Đây là hàm tính kích thước ảnh fill frame mà ảnh không bị biến dạng:

- (CGSize)getNewSize:(float)x yFrame:(float)y imageWith:(float)a imageHeight:(float)b {
    
    if (x == a && y == b) {     
        return CGSizeMake(a, b);
    }
    else if (x > a && y > b) {        
        if (x - a > y - b) {          
            a = y / b * a;
            b = y;
        } else {
            b = x / a * b;            
            a = x;
        }
    }
    else if (x < a && y < b) {        
        if (a - x > b - y) {          
            b = x / a * b;
            a = x;
        } else {                      
            a = y / b * a;
            b = y;
        }
    }
    else if (x < a && y > b) {        
        b = x / a * b;
        a = x;
    }
    else if (x > a && y < b) {        
        a = y / b * a;
        b = y;
    }
    else if (x == a) {
        a = y / b * a;
        b = y;
    }
    else if (y == b) {
        b = x / a * b;
        a = x;
    }
    return CGSizeMake(a, b);

}

Thứ Tư, 22 tháng 2, 2017

Hướng dẫn sử dụng cocoaPods

***Khái niệm:
- cocoaPods là công cụ quản lý các mã nguồn mở Object - C cho iOS và MacOSX

***Tại sao dùng coaPods?
- Đơn giản
- Dễ quản lý
- Ít bước

***Hướng dẫn cài đặt và sử dụng:
- B1: cài đặt gemRuby: mở terminal gõ lệnh: 
 
$ sudo gem install cocoapods


- B2: tạo file tên là "Podfile" trong thư mục chính của project cùng thư mục với file ".xcodeproj"

- B3: sửa nội dung Podfile:

     target 'ten_project' do
            pod 'ten_thu_vien'
     end


- B4: cd vào thư mục chứa Podfile gõ lệnh:

     $ pod install



- B5: chờ tải thư viện xong mởi file ".xcworkspace" và build để xem kết quả :)

Thứ Ba, 3 tháng 1, 2017

Xoá bỏ NSLog AQDefaultDevice (173): skipping input stream 0 0 0x0

1. Chọn Product/Scheme/Edit Scheme
2. Chọn Arguments
3. Thêm Environment Variable OS_ACTIVITY_MODE and set it to "disable"


*** Kết quả như hình:


Thứ Sáu, 18 tháng 11, 2016

Sử dụng UIScrollView xib

1. Kéo UIScrollView vào View và căn mép như hình




2. Kéo MainView vào UIScrollView căn các mép như 2 hình bên dưới





3. Kéo các View vào MainView là OK :)




Thứ Năm, 17 tháng 11, 2016

Timer Object C

***Timer Delay:

double delayInSeconds = 1.2;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    NSLog(@"Do some work");
});


***Timer Repeat:


- (void)viewDidAppear:(BOOL)animated {
    float delayTime = 1.0f;
    if (_timerUpdate == nil) {
        _timerUpdate = [NSTimer scheduledTimerWithTimeInterval:delayTime target:self selector:@selector(onTickUpdateIcon:) userInfo:nil repeats:YES];
    }
    [_timerUpdate fire];
}

- (void)onTickUpdateIcon:(NSTimer *)timer {
    // do something...
}

- (void)viewDidDisappear:(BOOL)animated {
    [_timerUpdate invalidate];
    _timerUpdate = nil;
}

Thứ Ba, 25 tháng 10, 2016

Các khai báo cơ bản trong Object c

- Khai báo hằng:

#define SWITCH_TYPE ((int) 2)
#define MY_STRING_CONSTANT @"my_constant"

- Sử dụng NSArray:

Khai báo:
NSArray *arrayOne = @[@"Mercedes-Benz", @"BMW", @"Porsche",
                    @"Opel", @"Volkswagen", @"Audi"];

NSArray *arrayTwo = [NSArray arrayWithObjects:@"Aston Martin",
                    @"Lotus", @"Jaguar", @"Bentley", nil];

NSLog(@"phần tử đầu tiên: %@", arrayOne[0]);
NSLog(@"phần tử đầu tiên: %@", [arrayTwo objectAtIndex:0]);

Kiểm tra phần tử:
if ([arrayOne containsObject:@"BMW"]) {
    NSLog(@"BMW is a German auto maker");
}

- Sử dụng NSMulableArray:
// khai báo
NSMutableArray *arrayOne = [NSMutableArray new];

NSMutableArray *arrayTwo = [NSMutableArray arrayWithObjects: @"Audi A6", @"BMW Z3", @"Audi Quattro", @"Audi TT", nil];

// sử dụng
[arrayOne addObject:@"new obj"];
[arrayOne removeLastObject];
[arrayOne removeAllObjects];
[arrayOne removeObjectAtIndex:0];

// Add BMW F25 to front
[arrayOne insertObject:@"BMW F25" atIndex:0];
    
// Remove BMW F25 from front
[arrayOne removeObjectAtIndex:0];
    
// Remove Audi Quattro
[arrayOne removeObject:@"Audi Quattro"];

// Change second item to Audi Q5
[arrayOne replaceObjectAtIndex:1 withObject:@"Audi Q5"];

...


Thứ Hai, 3 tháng 10, 2016

Customize UITableView

- Tạo CustomCell:
+ Tạo file xib:

+ sửa file ViewController.m:


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _uiTable.dataSource = self;
    _uiTable.delegate = self;
    
    [_uiTable registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellReuseIdentifier:@"MyCell"];

}

...

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    
    [cell.label setLineBreakMode:NSLineBreakByWordWrapping];
    cell.label.numberOfLines = 0;
    cell.label.text = _dataArray[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}
...

- Set background color:


- Căn trái sperator:

- Clear Footer operator:

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    // This will create a "invisible" footer
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    // To "clear" the footer view
    return [UIView new];

}

- Set height wrap label content:


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell" forIndexPath:indexPath];
    
    // label multi line:
    [cell.label setLineBreakMode:NSLineBreakByWordWrapping];
    cell.label.numberOfLines = 0;
    
    cell.label.text = _dataArray[indexPath.row];
    cell.backgroundColor = [UIColor clearColor];
    return cell;

}

// wrap label:
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewAutomaticDimension;

}

- Set background selected:

// change background color of selected cell
UIView *bgColorView = [[UIView alloc] init];
[bgColorView setBackgroundColor:[UIColor clearColor]];

[cell setSelectedBackgroundView:bgColorView];

- Khoá select

_contentTableView.allowsSelection = NO;

- Khoá kéo Scroll

_menuTableView.scrollEnabled = NO;

Thứ Bảy, 1 tháng 10, 2016

Đặt file .xib làm màn hình chính của ứng dụng

1. tạo project
2. New file như hình






3. Sửa nội dung file AppDelegate.m


#import "AppDelegate.h"
#import "MainViewController.h"

@interface AppDelegate ()

@property (strong, nonatomic) MainViewController *mainViewController;
@property (strong, nonatomic) UINavigationController *navController;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _navController = [[UINavigationController alloc] init];
    _navController.navigationBarHidden = YES;
    _mainViewController = [[MainViewController alloc] init];
    
    _window.rootViewController = [_navController initWithRootViewController:_mainViewController];
    
    return YES;

}

...