原意:你在非主線程動到了UI,可能會造成崩潰。
只要有UI前綴的,包含AlertView類跳窗,更改任何參數都要在主線程
dispatch_async(dispatch_get_main_queue(), ^{
[self updateUI];
});
dispatch_async(dispatch_get_main_queue(), ^{
[self updateUI];
});
myStruct dataA ; //ERROR
struct myStruct dataA ; //OK
struct myStruct *dataB = (struct myStruct *) dataA;
- (void)viewWillAppear:(BOOL)animated{
[self conn_server:URL1];
[NSThread sleepForTimeInterval:0.01f];//中間斷開
[self conn_server:URL2];
}
sudo rm -rf /System/Library/Java/JavaVirtualMachines/1.6.0.jdk
//加入觀察
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// register for keyboard notifications
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
//解除觀察
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// unregister for keyboard notifications while not visible.
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}
//鍵盤出現/消失:調整view
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary* info = [notification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGPoint itemOrigin = [editingTextField convertPoint:editingTextField.frame.origin toView:self.view];
CGFloat itemHeight = editingTextField.frame.size.height;
CGRect visibleRect = self.view.bounds;
visibleRect.size.height -= (keyboardSize.height+itemHeight);
//BOOL tmp =CGRectContainsPoint(visibleRect, itemOrigin);
//move up view if keyboard hide the textfield
if (!CGRectContainsPoint(visibleRect, itemOrigin)){
CGRect rect = self.view.frame;
rect.origin.y -= keyboardSize.height;
[UIView animateWithDuration:1.0 animations:^{
self.view.frame = rect;
}];
}
}
- (void)keyboardWillHide:(NSNotification *)notification {
CGRect rect = self.view.frame;
rect.origin.y= 64; //64 =navgation bar height
[UIView animateWithDuration:1.0 animations:^{
self.view.frame = rect;
}];
}
#pragma mark - textField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
editingTextField = textField;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
[textField resignFirstResponder];
}
dispatch_async(dispatch_queue_create("action", nil), ^{
//flag關掉thread會自己結束
while (actionFlag) {
[self prepareAction];
[NSThread sleepForTimeInterval:0.1f];
}
});
self.animationImageView.image = [UIImage animatedImageNamed:@"step_img" duration:0.5f];
[self.animationImageView startAnimating];