Świetne fragmenty kodu UIKit / Objective-C

Nowy w Objective-C iPhone/iPod touch / iPad development, ale zaczynam odkrywać wiele mocy w jednowierszowych kodach takich jak ten:

[UIApplication sharedApplication].applicationIconBadgeNumber = 10;

Który wyświetli tę charakterystyczną czerwoną plakietkę powiadomień na iphone ' ie z numerem 10.

Proszę udostępnić swoje ulubione jedno-lub dwu-linery w Objective-C dla iPhone / iPod touch / iPad tutaj. tylko publiczne API .

Author: Cat Man Do, 2010-06-10

5 answers

Otwórz adres URL w Safari

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com/"]];

Ukryj pasek stanu

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];

Wybierz numer telefonu (tylko iPhone)

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://9662256888"]];

Uruchom Apple Mail

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://[email protected]"]];

Stop responding to touch events

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

Aktywne zdarzenia dotykowe

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

Pokaż wskaźnik aktywności sieci

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

Ukryj wskaźnik aktywności sieci

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

Zapobiega iPhone przechodzi w tryb uśpienia

[UIApplication sharedApplication].idleTimerDisabled = YES;
 39
Author: RRUZ,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-17 20:29:08
  1. Wyświetl okno alertu:

    UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"Warning" message:@"too many alerts" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
    [alert show] 
    
  2. Pobierz ścieżkę do folderu Dokumenty:

    NSArray*  paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString* documentsDirectory = [paths objectAtIndex:0];
    
  3. Wciśnij inny kontroler widoku na pasek nawigacyjny:

    [self.navigationController pushViewController:anotherVC animated:YES];
    
  4. W przeciwieństwie do innych wersji alpha, nie jest ona dostępna w wersji 3.0.]}
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];  // fade away over 1 seconds
    [aView setAlpha:0]; 
    [UIView commitAnimations];                      
    
  5. Pobierz nazwę aplikacji

    self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
    
  6. Zmień pasek stanu na czarny

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
    
  7. Zmiana stylu paska nawigacyjnego (z kontroler widoku):

    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
  8. Zapisz NSString do NSUserDefaults:

    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:loginName forKey:kUserLoginName];
    
  9. Pobierz NSString z NSUserDefaults:

    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    

    NSString * loginName = [defaults stringForKey:kUserLoginName];

  10. Sprawdź, czy obiekt obsługuje metodę przed jej wywołaniem:

    if ([item respondsToSelector:@selector(activateBOP:)]) {
        [item activateBOP:closeBOP];
    }
    
  11. Loguj nazwę klasy i funkcji:

    NSLog(@"%s", __PRETTY_FUNCTION__);
    
  12. Dodaj zaokrąglone rogi i / lub obramowanie wokół dowolnego elementu UIView (self)

    self.layer.borderColor  = [UIColor whiteColor].
    self.layer.cornerRadius = 8;     // rounded corners
    self.layer.masksToBounds = YES;  // prevent drawing outside border
    
  13. Otwórz aplikację Google Maps z kierunkami między dwoma punktami lat / long

    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f&dirflg=d", start.latitude, start.longitude, finish.latitude, finish.longitude];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; 
    
 14
Author: progrmr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-19 04:47:43

Zapisz bool do domyślnych wartości użytkownika

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Yes Bool"];

Skopiuj plik z x do y

[[NSFileManager defaultManager] copyItemAtPath:x toPath:y error:nil];

Wyświetl nowy Widok

[self presentModalViewController:(UIViewController *) animated:YES];

Metoda dotknięcia ekranu

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {}

Pobierz katalog dokumentów

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

Załaduj URL

[MyWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://couleeapps.hostei.com"]]];  

Pobierz aktualną datę i godzinę:

NSCalendar *gregorian = [NSCalendar currentCalendar];
NSDateComponents *dateComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:[NSDate date]];

Własny typ enum:

typedef enum {
    a = 0, b = 1, c = 2
} enumName;

Kwarcowy Łuk rysunkowy

CGContextRef ctxt = UIGraphicsGetCurrentContext();
CGContextAddArc(ctxt, x, y, radius, startDeg, endDeg);
 10
Author: HiGuy 'CouleeApps' Smith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-21 16:20:59

Spraw, aby urządzenie wibrowało:

AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);

Otwórz aplikację wiadomości z określonym numerem telefonu:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:123456789"]];

Przestań reagować na zdarzenia dotykowe:

[[UIApplication sharedApplication] beginIgnoringInteractionEvents];

Zacznij od nowa:

[[UIApplication sharedApplication] endIgnoringInteractionEvents];

I wreszcie pojedyncza linia kodu przeglądarki :

[[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: [urlText stringValue]]]];
 8
Author: DenverCoder9,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-10 21:42:13

Zmień tytuł na przycisku Wstecz w widoku UINavigationView. Użyj tego kodu w kontrolerze UINavigationController przed naciśnięciem widoku

UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style: UIBarButtonItemStyleBordered target:nil action:nil];


self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
 5
Author: Kevin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-06-17 20:34:17