iphone 开发小技巧备忘

1. 在uiviewcontroller声明nsstring property的方法:
NSString *groupName;
@property (nonatomic, assign) NSString *groupName;

2.Setting the back button title of a UINavigationItem
The following code will set the back button title of the current UINavigationItem:

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
[self.navigationItem setBackBarButtonItem:backButton];
[backButton release];


The target: and action: parameters are definitely ignored (replaced by the default back button behavior) and the style also seems to be ignored because the back button retains its pointed left side.

This next block of code looks like it should do the same thing but does not work:

self.navigationItem.backBarButtonItem.title = [NSString stringWithString:@"Back"];

注意:一定要在当前页面设置backbarbuttonitem!!
MAJOR CAVEAT: As pointed out in this thread, "the back button is 'owned' by the previous view on the stack." In other words, the setBackBarButtonItem method should be called on the view that the user is navigating away from, rather than the view above which this button is actually displayed.

3. 调整tableviewcell 背景色

UITableViewCell *bgView = [[UITableViewCell alloc] initWithFrame:CGRectZero];
bgView.backgroundColor=indexPath.row % 2? [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1]: [UIColor whiteColor];
bgView.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"disclosure.png"]];
cell.backgroundView=bgView;
return cell;


4. ipad 背景透明

tableView.backgroundColor = [UIColor clearColor];
在iPhone上可以透明,但是在iPad上无效,
解决方案:ttable.backgroundView.alpha = 0;

5. 图标 资源
Ever reach a brick wall in designing your app due to a shortage of artwork? I ran into this situation this morning, and figured I’d share what I’ve found for others that might be similarly design handicapped.

iPhone Tab Bar Icons
GlyFX has a few sets available for purchase at [url]http://www.glyfx.com/shop/listing/iphone/[/url]
GLYPHISH has a large set for free at [url]http://glyphish.com/[/url]
Quality Icons Blog has a few free gaming focused icons available here [url]http://qualityicons.blogspot.com/2009/08/iphone-tab-bar-gaming-icons.html[/url]
eddit has a 160+ icon set you can purchase here [url]http://www.eddit.com/shop/iphone_ui_icon_set/[/url]
kombine has a 130+ icon set for sale at [url]http://www.kombine.net/icon-store/iphone-tab-bar-icons[/url]
Just remember your app can get rejected for the incorrect use of icons or breaking the iPhone Human Interface Guidelines. I found the blog post “icon fail” by Under the Bridge extremely helpful in understanding the importance of selecting your icons.

6. How to make your own tab bar icon
[url]http://www.youtube.com/watch?v=YUWMeJq9f-8[/url]
[url]http://www.aha-soft.com/faq/ios-toolbar-icons.htm[/url]
[url]http://speckyboy.com/2010/04/30/iphone-and-ipad-development-gui-kits-stencils-and-icons/[/url]

7. 各种图标尺寸
[url]http://www.axialis.com/tutorials/make-ipad-icons.html#t2[/url]

8. Resizing a UITableViewCell to Hold Variable Amounts of Text
[url]http://www.raddonline.com/blogs/geek-journal/iphone-sdk-resizing-a-uitableviewcell-to-hold-variable-amounts-of-text/[/url]

9. RoundedUITableView for iOS
[url]http://www.cocoacontrols.com/platforms/ios/controls/roundeduitableview[/url]

10. 从navigationcontroller隐藏toolbar时隐藏动画会产生阴影,解决办法时不使用navigationcontroller自带的toolbar,自己在uiview中添加toolbar:
- (void)viewDidLoad
{
// Add a toolbar to the view
CGRect toolbarFrame = CGRectMake(0, 372, 320, 44);
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];

UIBarButtonItem *compassButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"compass.png"]
style:UIBarButtonItemStyleBordered
target:self
action:@selector(zoomToCurrentLocation)];

compassButton.width = 30.0f; // make the button a square shape
[myToolbar setItems:[NSArray arrayWithObject:compassButton] animated:NO];
[compassButton release];

[self.view addSubview:myToolbar];
[super viewDidLoad];
}


11. How to compare if two objects are really the same object?
The == operator tests whether the two expressions are the same pointer to the same object. Cocoa calls this relation “identical” (see, for example, NSArray's indexOfObjectIdenticalTo:).

To test whether two objects are equal, you would send one of them an isEqual: message (or a more specific message, such as isEqualToString:, if it responds to one), passing the other object. This would return YES if you really only have one object (equal to itself, obviously) or if you have two objects that are equal. In the latter case, == will evaluate to NO.

12. IOS支持的所有字体 font
[url]http://iosfonts.com/[/url]

13. Advanced App Tricks
[url]http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html[/url]

14. 输入密码界面:
https://github.com/Koolistov/Passcode

http://code.google.com/p/kpasscode/

15. 程序和iTunes间共享文件
[url]http://www.alterplay.com/ios-dev-tips/2010/11/file-sharing-from-app-to-itunes-is-too-easy.html[/url]

16. [url]http://stackoverflow.com/questions/7989968/iphone-loading-view-slide-effect[/url]


1、判断一个view有没有superView
if ( [scrollView superviews] )
{
[scrollView removeFromSuperView];
}
else
{
[self.view addSubView:scrollView];
}
2、去除tableview的蓝色选中
吧tableview的Selection改为No Selection即可。
3、如何调用父view的controller里面的方法
[[self superview ].nextResponder method];
[[[self superview ] nextResponder] method];
[self.nextResponder method];
上面的都可以,看情况使用,使用的时候最好判断一下。

官方解释
UIView implements this method by returning the UIViewController object that manages it (if it has one) or its superview (if it doesn’t); UIViewController implements the method by returning its view’s superview; UIWindow returns the application object, and UIApplication returns nil.
4、如何隐藏tab bar controller 的tab bar
在Iphone编程中,很多人喜欢把Tab bar controller和Nav 一起混合使用,在点击了Tab bar上面的一个按钮切换到另一个view的时候,界面上的Tab Bar 没有消失.

导致view有部分的界面给遮挡了.所以需要把Tab Bar 给隐藏掉,隐藏代码如下:

self.newView = [[newViewController alloc] init];
newView.hidesBottomBarWhenPushed=YES;
[self.navigationController presentModalViewController:newView animated:YES];

在建立新的View的时候加入

newView.hidesBottomBarWhenPushed=YES;

即可使Tab Bar消失掉.

5、

UISearchBar 动作处理

/*取消按钮*/
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
[self doSearch:searchBar];
}

/*键盘搜索按钮*/
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
[searchBar resignFirstResponder];
[self doSearch:searchBar];
}

/*搜索*/
- (void)doSearch:(UISearchBar *)searchBar{
...
}
UISearchBar上按钮的默认文字为Cancel,如果想改为其他文字请调用一下代码
for(id cc in [searchtext subviews]){
if([cc isKindOfClass:[UIButton class]]){
UIButton *btn = (UIButton *)cc;
[btn setTitle:@"取消" forState:UIControlStateNormal];
}
}
注意:需要在.h文件种添加
<UISearchBarDelegate>,然后在.m文件种设置SearchBar的委托为:
self.SearchBar.delegate =self; 才可以生效。

6、控制Default.png图片的显示时间
直接在delegate里
sleep(想延长的秒数);

7、将UISearchBar 键盘上的Search键改成其他名字:
UITextField *searchField = [[search subviews] lastObject];
[searchField setReturnKeyType:UIReturnKeyDone];

8、 UITabBarController点击下面TabBarItem菜单时,如何重新加载?
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//NSLog(@"加载一次");
//在此添加每次加载的时候要执行的内容
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值