Month: February 2013

  • Get file size using NSFileManager

    To get file size in bytes using NSFileManager do the following:

    NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:_filePath error:nil];
    _size = [attrs fileSize];
    
  • How to add photos to iOS simulator

    This solution worked for me under iOS 6.1:

    Close the simulator if it is running xcode project.
    Run simulator from the dock by clicking on it.
    Drag & drop the image into simulator.
    Tap and hold the image (opened in safari) and select the save option.
    You are done.

    Taken from here.

  • UITextField edge insets

    To change edge insets in UITextField, create a new subclass and override these methods:

    // placeholder position
    - (CGRect)textRectForBounds:(CGRect)bounds {
        return CGRectInset(bounds, 0, 10);
    }
    
    // text position
    - (CGRect)editingRectForBounds:(CGRect)bounds {
        return CGRectInset(bounds, 0, 10);
    }
    

    Here 0 is dx, 10 is dy.

    Found this nice solution here

  • How to make UITextField in UITableView become first responder

    I loadView create a UITextField ivar.

    - (void)loadView
    {
        [super loadView];
    
        _userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 10, 280, 30)];
        _userNameTextField.placeholder = @"Введите имя пользователя";
        [_userNameTextField setClearButtonMode:UITextFieldViewModeWhileEditing];
        _userNameTextField.returnKeyType = UIReturnKeyGo;
        _userNameTextField.delegate = self;
        _userNameTextField.tag = 1;
    ...
    

    In ViewDidAppear make it first responder:

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
        
        [_userNameTextField becomeFirstResponder];
    }
    

    In cellForRow add it as a subview:

    - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell *cell = [[UITableViewCell alloc] init];
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                [cell addSubview:_userNameTextField];
    
  • Отлавливание Shake событий на iOS

    Взять любое кастмное View на данном экране. Добавить в его коде:

    - (BOOL)canBecomeFirstResponder
    {
        return YES; // Для отлавления Shake события
    }
    

    При загрузке данного View Controller и инциализации данного View делать следующее:

    [_composeButton becomeFirstResponder]; // Для отлавления Shake события
    

    Добавить следующий метод в данном View Controller.

    // Для отлавления Shake события
    - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
    {
        // Обработка события встряхивания
        if ( event.subtype == UIEventSubtypeMotionShake ) {
            NCLog(@"Shaked");
            
            if ( UD_BOOL(UDKeyNewMessageOnShake) ) [self showComposeEmailViewControllerInMode:kComposeModeNew WithMCMessageFromIndexPath:nil];
        }
    }
    

    Все!

  • performSelector may cause a leak because its selector is unknown

    This is very popular question in StackOverflow.

    However, the best answer for XCode 4.6 is this:
    “In your project Build Settings, under Other Warning Flags (WARNING_CFLAGS), add
    -Wno-arc-performSelector-leaks

    Now just make sure that the selector you are calling does not cause your object to be retained or copied.”

    “Note you can add the same flag for specific files rather than the entire project. If you look under Build Phases->Compile Sources, you can set per file Compiler Flags (just like you want to do for excluding files from ARC). In my project just one file should use selectors this way, so I just excluded it and left the others.”

    Code snippet:

    -Wno-arc-performSelector-leaks