Ś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 .
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;
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
-
Wyświetl okno alertu:
UIAlertView* alert = [[[UIAlertView alloc] initWithTitle:@"Warning" message:@"too many alerts" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; [alert show]
-
Pobierz ścieżkę do folderu Dokumenty:
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString* documentsDirectory = [paths objectAtIndex:0];
-
Wciśnij inny kontroler widoku na pasek nawigacyjny:
[self.navigationController pushViewController:anotherVC animated:YES];
-
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];
-
Pobierz nazwę aplikacji
self.title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
-
Zmień pasek stanu na czarny
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
-
Zmiana stylu paska nawigacyjnego (z kontroler widoku):
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
-
Zapisz NSString do NSUserDefaults:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:loginName forKey:kUserLoginName];
-
Pobierz NSString z NSUserDefaults:
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
NSString * loginName = [defaults stringForKey:kUserLoginName];
-
Sprawdź, czy obiekt obsługuje metodę przed jej wywołaniem:
if ([item respondsToSelector:@selector(activateBOP:)]) { [item activateBOP:closeBOP]; }
-
Loguj nazwę klasy i funkcji:
NSLog(@"%s", __PRETTY_FUNCTION__);
-
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
-
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]];
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);
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]]]];
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];
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