iOS – Right way to handle keyboard appear/disappear in iOS 5
Posted by AllenWei | Posted in iOS Dev | Posted on 10-05-2012-05-2008
View Comments
Because after iOS 5, keyboard will have dynamic height,event more split and doc feature will position keyboard in directly location, so the good way to handle keyboard is:
@end
@implementation KeyboardScrollView
-(void)drawRect:(CGRect)rect {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardFrameChanged:)
name:UIKeyboardDidChangeFrameNotification object:nil];
[super drawRect:rect];
}
- (void)keyboardFrameChanged:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGPoint from = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].origin;
CGPoint to = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].origin;
float height = 0.0f;
if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) {
height = to.x - from.x;
} else {
height = to.y - from.y;
}
[self setContentSize:CGSizeMake(self.frame.size.width, self.frame.size.height + height)];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches){
if (touch.view == self){
[self findAndResignFirstResponder];
}
}
[super touchesBegan:touches withEvent:event];
}
-(void)findAndResignFirstResponder{
for (UIView *aView in [self subviews]){
if ([aView isFirstResponder] ) {
[aView resignFirstResponder];
}
}
}
-(void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidChangeFrameNotification object:nil];
}
iOS – Modern Logger Solution for iOS NSLogger
Posted by AllenWei | Posted in iOS Dev | Posted on 13-04-2012-05-2008
View Comments
NSLogger is a replacement of NSLog which provide an GUI app to see and filter your log. you can log text, binary even image.
But there is no log level control.
You can use LibComponentLogging-NSLog LibComponentLogging extension
or use your custom level control macro:
#define BPLOGLEVEL_DEBUG 0
#define BPLOGLEVEL_INFO 1
#define BPLOGLEVEL_WARNING 2
#define BPLOGLEVEL_ERROR 3
#ifndef BPLOGLEVEL
#define BPLOGLEVEL BPLOGLEVEL_DEBUG
#endif
// you can refined tag in your file, using following code
//
// #undef BPLogTag
// #define BPLogTag @"OrderMessageReceiver"
#define BPLogTag @"App"
#ifdef DEBUG
#define BPLog(level, tag, ...) \
if (level >= BPLOGLEVEL) { \
LogMessageF(__FILE__,__LINE__,__FUNCTION__, tag, level,__VA_ARGS__); \
}
#else
#define BPLog(level, tag, ...) ((void)0)
#endif // #ifdef DEBUG
#define BPLogError(...) BPLog(BPLOGLEVEL_ERROR, BPLogTag, __VA_ARGS__)
#define BPLogWarning(...) BPLog(BPLOGLEVEL_WARNING, BPLogTag, __VA_ARGS__)
#define BPLogInfo(...) BPLog(BPLOGLEVEL_INFO, BPLogTag, __VA_ARGS__)
#define BPLogDebug(...) BPLog(BPLOGLEVEL_DEBUG, BPLogTag, __VA_ARGS__)
iOS – Be careful NSArray indexOfObject method
Posted by AllenWei | Posted in iOS Dev | Posted on 22-03-2012-05-2008
View Comments
if you want to determine if an object in an array, please be careful.
see following code:
NSInteger index = [array indexOfObject: @"not exist"]
if (index >= 0) {
//always be true
}
// you must use
if (index != NSNotFound) {
}
Fix Code Sense and Syntax Highlighting for Xcode
Posted by AllenWei | Posted in iOS Dev | Posted on 21-03-2012-05-2008
View Comments
Sometimes syntax highlighting does not work on Xcode 4.
We can do it with following steps:
- Go to Organizer project panel
- Choose your project
- Click “Delete” button after the Derived Data on the right panel.
Xcode will start to rebuild code sense of your project.
Use Rails NumberHelper in Sinatra application
Posted by AllenWei | Posted in ruby | Posted on 19-03-2012-05-2008
View Comments
first, see if actionpack in your gemfile.lock.
if you are using action mailer, you should have actionpack
put follow code in your Sinatra application
include ActionView::Helpers::NumberHelper
iOS – Determine Whether Current View is a Modal
Posted by AllenWei | Posted in iOS Dev | Posted on 13-03-2012-05-2008
View Comments
UIViewController+ModalAdditions.h
-(BOOL)isModal;
@end
UIViewController+ModalAdditions.m
@implementation UIViewController (ModalAdditions)
-(BOOL)isModal {
BOOL isModal = ((self.parentViewController && self.parentViewController.modalViewController == self) ||
//or if I have a navigation controller, check if its parent modal view controller is self navigation controller
( self.navigationController && self.navigationController.parentViewController && self.navigationController.parentViewController.modalViewController == self.navigationController) ||
//or if the parent of my UITabBarController is also a UITabBarController class, then there is no way to do that, except by using a modal presentation
[[[self tabBarController] parentViewController] isKindOfClass:[UITabBarController class]]);
//iOS 5+
if (!isModal && [self respondsToSelector:@selector(presentingViewController)]) {
isModal = ((self.presentingViewController && self.presentingViewController.modalViewController == self) ||
//or if I have a navigation controller, check if its parent modal view controller is self navigation controller
(self.navigationController && self.navigationController.presentingViewController && self.navigationController.presentingViewController.modalViewController == self.navigationController) ||
//or if the parent of my UITabBarController is also a UITabBarController class, then there is no way to do that, except by using a modal presentation
[[[self tabBarController] presentingViewController] isKindOfClass:[UITabBarController class]]);
}
return isModal;
}
@end
Fix Rcov After Upgrade Rspec to 2.8
Posted by AllenWei | Posted in RubyOnRails | Posted on 14-01-2012-05-2008
View Comments
Rcov doesn’t work after I upgrade rspec to 2.8,
here is a quick fix
add following code into your spec_helper
Use Paperclip in rack app like sinatra and how to write test
Posted by AllenWei | Posted in ruby, Tips | Posted on 08-11-2011-05-2008
View Comments
You’ll find rack parse uploaded file in different format to Rails
Here is the format in rack app.
You can simply covert it to right format.
yourModel = YourModel.new(:image => to_paperclip(params['image']))
yourModel.save
end
def to_paperclip(image)
paperclip = {}
paperclip['tempfile'] = image[:tempfile]
paperclip['filename'] = image[:filename]
paperclip['content_type'] = image[:type]
paperclip['size'] = image[:tempfile].size
paperclip
end
How to test
file = Rack::Test::UploadedFile.new(filename, "image/png")
post "/api/v1/some_api", {:image => file}
# should have image uploaded
iOS – Cocoa Non-Blocking sleep wait background task
Posted by AllenWei | Posted in iOS Dev | Posted on 30-10-2011-05-2008
View Comments
If you have some async task running in background, and you need wait this background task until it finished, code below will help u:
while (![self isFinished] && [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:loopUntil]) {
loopUntil = [NSDate dateWithTimeIntervalSinceNow:0.1];
}


