Skip to content

Instantly share code, notes, and snippets.

@xxd
Created May 17, 2012 07:04
Show Gist options
  • Save xxd/2717120 to your computer and use it in GitHub Desktop.
Save xxd/2717120 to your computer and use it in GitHub Desktop.
iOS SDK控件:UITableView,UITextView,NSTimer,UISwitch,NSBundle,UIButton,UINavigation,UIImage,Segue
//*******************
// NSBundle ofType
//*******************
NSString *textUrl = [[NSBundle mainBundle] pathForResource:@"myText" ofType:@"txt"];
textView.text = [NSString stringWithContentsOfFile:textUrl encoding:NSUTF8StringEncoding error:nil];
//NSTimer的用法
//iPhone为我们提供了一个很强大得时间定时器 NSTimer,它可以完成任何定时功能:
//我们使用起来也很简单,只要记住三要素就可以,具体得三要素是:时间间隔NSTimeInterval浮点型,事件代理delegate和事件处理方法@selector();就可以用
//1 +(NSTimer *)scheduledTimerWithTimeIn
//2 terval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
//来初始化一个 时间定时器 下面我写了一个很简单得例子:
-(void)initTimer
{
//时间间隔4 NSTimeInterval timeInterval =1.0;
//定时器6 NSTimer
showTimer =[NSTimer scheduledTimerWithTimeInterval:maxShowTime
target:self
selector:@selector(handleMaxShowTimer:)
userInfo:nil
repeats:NO];
}
//触发事件13 -(void)handleMaxShowTimer:(NSTimer *)theTimer
{
NSDateFormatter dateFormator =[[NSDateFormatter alloc] init];
dateFormator.dateFormat =@"yyyy-MM-dd HH:mm:ss";
NSString *date =[dateformater stringFromDate:[NSDate date]];
if([date isEqualToString:@"2010-11-09 23:59:59"])
{
UIAlertView *alert =[[UIAlertView alloc] initWithTitle:TITLE_NAME
message:@"现在马上就有新的一天了!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:CONFIRM_TITLE, nil];
[alert show];
}
}
//获取objectAtIndex:self.tableView.indexPathForSelectedRow.row
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DetailViewController *detailController =segue.destinationViewController;
ScaryBugDoc *bug = [self.bugs objectAtIndex:self.tableView.indexPathForSelectedRow.row];
detailController.detailItem = bug;
}
//***************
//调整UIButton中文字的位置
//***************
CGRect btnRect = CGRectMake(360 * 0.5f,352 * 0.5f, 107,49);
m_iknowBtn = [[UIButton alloc] initWithFrame:btnRect];
[m_iknowBtn setTitle:FoolLocalizedString(@"知道了", nil) forState:UIControlStateNormal];
[m_iknowBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
m_iknowBtn.titleLabel.font = [UIFont systemFontOfSize:18];
m_iknowBtn.titleLabel.adjustsFontSizeToFitWidth = YES;
m_iknowBtn.titleLabel.minimumFontSize = 5;
[m_iknowBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 18, 0)];
[m_iknowBtn setBackgroundImage:[UIImage imageNamed:@"ipad_goodluck_iknow_btn.png"] forState:UIControlStateNormal];
[m_iknowBtn addTarget:self action:@selector(closeLayer) forControlEvents:UIControlEventTouchUpInside];
//***************
//如何使用UIButton的(id)sender传值
//***************
// 1 首先在tableView cellForRowAtIndexPath:方法中给tag赋值
answerCell.nameBtn.tag = [indexPath row];
// 2 然后用sender获取
-(void)userDetail:(id)sender{
UIButton *button = (UIButton *)sender;
UserDetailViewController *userDetail = [self.storyboard instantiateViewControllerWithIdentifier:@"UserDetail"];
NSDictionary *dicPost = [_listData objectAtIndex:button.tag];
........
}
--EOF--
//***************
// UIButton背景色
//***************
// UIButton有setBackgroundColor的方法,但并不能使背景变色。这个网站介绍了一种利用setBackgroundImage:forState:方法设置背景颜色
+ (void)backgroundTurnRedForButton:(UIButton *)button {
CGSize size = button.frame.size;
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
view.layer.cornerRadius = 6;
view.clipsToBounds = true;
view.backgroundColor = [UIColor redColor];
UIGraphicsBeginImageContext(size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[button setBackgroundImage:screenImage forState:UIControlStateNormal];
}
//***************
// 添加UIButton
//***************
//1. interface builder上添加按钮
//2 .h
@property (weak, nonatomic) IBOutlet UIButton *syncDropbox;
//然后control+drag到interface builder上添加的按钮
//基本步骤
@synthesize syncDropbox;
- (void)viewDidLoad
{
[super viewDidLoad];
..........
..........
[syncDropbox addTarget:self action:@selector(syncMarkdownFile) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:syncDropbox];
..........
..........
}
- (void)syncMarkdownFile{
..........
..........
}
//自定义按钮
UIButton *addQuestion = [[UIButton alloc] initWithFrame: CGRectMake(250, 400, 40, 40)];
[self.view addSubview:addQuestion];
[addQuestion setImage:[UIImage imageNamed:@"MenuCompose.png"] forState:UIControlStateNormal];
[addQuestion setImage:[UIImage imageNamed:@"MenuComposePressed.png"] forState:UIControlStateSelected];
[addQuestion setBackgroundColor:[UIColor colorWithRed:173 green:86 blue:78 alpha:0]];
//另一种方法
UIButton *Btn;
CGRect frame;
Btn = [[UIButton buttonWithType:UIButtonTypeCustom] retain]; //按钮的类型
[Btn setImage:[UIImage imageNamed:@“aaa.png”] forState:UIControlStateNormal];//设置按钮图片
Btn.tag = 10;
frame.size.width = 59; //设置按钮的宽度
frame.size.height = 59; //设置按钮的高度
frame.origin.x =150; //设置按钮的位置
frame.origin.y =260;
[Btn setFrame:frame];
[Btn setBackgroundColor:[UIColor clearColor]];
[Btn addTarget:self action:@selector(btnPressed:)forControlEvents:UIControlEventTouchUpInside]; //按钮的单击事件
[self.view addSubview:Btn];
[Btn release];
-(void)btnPressed:(id)sender {
//在这里实现按钮的单击事件
}
//////////////////IOS 自定义导航栏标题和返回按钮标题//////////////////
//////////////////IOS中自定义导航栏标题和按钮:
UILabel *titleText = [[UILabel alloc] initWithFrame: CGRectMake(160, 0, 120, 50)];
titleText.backgroundColor = [UIColor clearColor];
titleText.textColor=[UIColor whiteColor];
[titleText setFont:[UIFont systemFontOfSize:17.0]];
[titleText setText:@"XXX"];
self.navigationItem.titleView=titleText;
[titleText release];
//////////////////IOS中自定义导航栏返回按钮:(放在pushViewController之前)
UIBarButtonItem *backItem=[[UIBarButtonItem alloc]init];
backItem.title=@"后退";
backItem.tintColor=[UIColor colorWithRed:129/255.0 green:129/255.0 blue:129/255.0 alpha:1.0];
self.navigationItem.backBarButtonItem = backItem;
[backItem release];
//////////////////IOS中自定义导航栏右边按钮
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]
initWithTitle:@"回到首页"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(callModalList)];
rightButton.image=[UIImage imageNamed:@"right_button.png"];
rightButton.tintColor=[UIColor colorWithRed:74/255.0 green:74/255.0 blue:74/255.0 alpha:1.0];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, 79, 29.0);
[backButton setImage:[UIImage imageNamed:@"button_back.png"] forState:UIControlStateNormal];
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
}
//在IOS开发中,默认的IOS UINavigationController 有导航功能,但在混合视图开发的时候,很多时候会隐藏导航栏,这个时候我们需要导航的返回按钮。(正向导航 pushViewController,大家都熟悉)
//如:在一个需要返回的.m 文件中定义一个方法
-(void)backXXX {
[self.navigationController popToViewController: [self.navigationController.viewControllers objectAtIndex: 1] animated:YES];
//这里的1 标识导航器的ViewControll 的顺序位置,1 标识第二级
}
//在混合视图开发中,还有一个需要注意的是善于用父类控件,如self.parentViewController.
//sing NSData to store Pic from a Url
NSURL *url = [NSURL URLWithString: [dicPost objectForKey:@"avatar_url"]];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
//***************
// 增加title
//***************
self.navigationItem.title = [NSStringstringWithFormat:@"%@个答案",[listDataobjectForKey:@"answers_count"]];
//***************
//Navigation Move in <-- this way
//***************
[self.navigationController popViewControllerAnimated:YES];
//***************
//View Move in ↓ this way
//***************
[self dismissViewControllerAnimated:YES completion:nil];
//***************
//toolbar
//***************
self.navigationController.toolbarHidden=NO;
UIBarButtonItem *lastPage=[[UIBarButtonItem alloc]initWithTitle:@"上一页" style:UIBarButtonItemStyleDone target: self action:nil];
UIBarButtonItem *nextPage=[[UIBarButtonItem alloc]initWithTitle:@"下一页" style:UIBarButtonItemStyleDone target:self action:nil];
[self.navigationController.toolbar setItems:[NSArray arrayWithObjects:lastPage,nextPage,nil] animated:YES];
//***************
//把这段代码加到 viewdidload里面 之后 当前页面只有一个空空的toolbar 没有出现 我定义的上一页和下一页的按钮,解决方案是:
//***************
[self setToolbarItems:[NSArray arrayWithObjects:lastPage,nextPage,nil] animated:YES];
//***************
//添加按钮
//***************
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]
initWithTitle:@"添加答案"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(toAnswer)];
self.navigationItem.rightBarButtonItem = rightButton;
self.navigationItem.title=@"问题详情";
//***************
//UINavigationBar的后退按钮
//***************
//self.navigationItem.leftBarButtonItem和self.navigationItem.rightBarButtonItem比较常用。
//self.navigationItem.backBarButtonItem今天才是头上一次看到。
//当navigationBar的标题比较长的时候,进入下一层时,后退按钮要占掉近一半的navigation bar的空间。所以这时候在进入下一层表格时,把后退按钮适当的改一下才不会显得奇怪。具体方法是在进入前的table view的didSelectRowAtIndexPath加入相应操作:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Home", @"") style:UIBarButtonItemStylePlain target:nil action:nil];
[self.navigationItem setBackBarButtonItem:cancelButton];
.....
}
//***************
//隐藏上框
//***************
self.navigationController.navigationBarHidden=YES;
//***************
// 添加UISwitch
//***************
UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectMake(4.0f, 16.0f, 100.0f, 28.0f)];
switchView.on = NO;//初始为Off
[switchView addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
//********************************
//如何让UITableViewCell滚动到视图顶端
//********************************
- (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
- (void)scrollToNearestSelectedRowAtScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated;
这两个方法都可以用,第一个函数是吧当前行滚动到指定位置,(UITableViewScrollPosition)scrollPosition-这个有四个选择:top,mid,bot,,,,,第二个函数是滚动到最近的位置
//********************************
//多线程获取UITableView Image的例子
//********************************
// 经常我们会用tableView显示很多条目, 有时候需要显示图片, 但是一次从服务器上取来所有图片对用户来浪费流量, 对服务器也是负担.最好是按需加载,即当该用户要浏览该条目时再去加载它的图片.重写如下方法
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
UIImage *image = [self getImageForCellAtIndexPath:indexPath]; //从网上取得图片
[cell.imageView setImage:image];
}
// 这虽然解决了延时加载的问题, 但当网速很慢, 或者图片很大时(假设,虽然一般cell中的图很小),你会发现程序可能会失去对用户的响应.原因是UIImage *image = [self getImageForCellAtIndexPath:indexPath]; 这个方法可能要花费大量的时间,主线程要处理这个method.所以失去了对用户的响应.所以要将该方法提出来:
- (void)updateImageForCellAtIndexPath:(NSIndexPath *)indexPath
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *image = [self getImageForCellAtIndexPath:indexPath];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
[cell.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
[pool release];
}
//然后再新开一个线程去做这件事情
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
[NSThread detachNewThreadSelector:@selector(updateImageForCellAtIndexPath:) toTarget:self withObject:indexPath];
}
// 同理当我们需要长时间的计算时,也要新开一个线程去做这个计算以避免程序处于假死状态。以上代码只是示例, 还可以改进的更多, 比如从网上down下来一次后就将图片缓存起来,再次显示的时候就不用去下载.
//***************************
//打开tableview到最后一列
//***************************
[self.chatTableView reloadData];
int lastRowNumber = [self.chatTableView numberOfRowsInSection:0] - 1;
NSIndexPath* ip = [NSIndexPath indexPathForRow:lastRowNumber inSection:0];
[self.chatTableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionTop animated:NO];
//***************************
//UITableView 基本使用方法总结
//***************************
//1.Controller需要实现两个delegate,分别是 UITableViewDelegate 和 UITableViewDataSource
//2.然后 UITableView对象的 delegate 要设置为 self。
//3. 然后就可以实现这些delegate的一些方法拉。
////(1)这个方法返回 tableview 有多少个section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
////(2)这个方法返回对应的section有多少个元素,也就是多少行。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
(3)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
////这个方法返回指定的 row 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
////这个方法返回指定的 section的header view 的高度。
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
////这个方法返回指定的 section的footer view 的高度。
//(4)返回指定的row 的cell。这个地方是比较关键的地方,一般在这个地方来定制各种个性化的 cell元素。这里只是使用最简单最基本
//的cell 类型。其中有一个主标题 cell.textLabel 还有一个副标题cell.detailTextLabel, 还有一个 image在最前头 叫
//cell.imageView. 还可以设置右边的图标,通过cell.accessoryType 可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头,
//还是勾勾标记。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";
UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
if (cell == nil)
{
// Create a cell to display an ingredient.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:showUserInfoCellIdentifier]
autorelease];
}
// Configure the cell.
cell.textLabel.text=@"签名";
cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding];
//设置cell线的颜色,背景颜色
tableView.separatorColor = [UIColor lightGrayColor];
cell.contentView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
tableView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
cell.textLabel.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
}
//4.1 如果想让cell中的image固定大小,需要在你的cell view controller中加入以下方法
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.bounds = CGRectMake(0,0,75,75);
self.imageView.frame = CGRectMake(0,0,75,75);
self.imageView.contentMode = UIViewContentModeScaleAspectFit;
CGRect tmpFrame = self.textLabel.frame;
tmpFrame.origin.x = 77;
self.textLabel.frame = tmpFrame;
tmpFrame = self.detailTextLabel.frame;
tmpFrame.origin.x = 77;
self.detailTextLabel.frame = tmpFrame;
}
////(5)返回指定的 section 的header的高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section ==0)
return 80.0f;
else
return 30.0f;
}
////(6)返回指定的section 的 header 的 title,如果这个section header 有返回view,那么title就不起作用了。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (tableView == tableView_)
{
if (section == 0)
{
return @"title 1";
}
else if (section == 1)
{
return @"title 2";
}
else
{
return nil;
}
}
else
{
return nil;
}
}
////(7)返回指定的 section header 的view,如果没有,这个函数可以不返回view
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"
owner: self
options: nil] lastObject];
else
{
return nil;
}
}
////(8)当用户选中某个行的cell的时候,回调用这个。但是首先,必须设置tableview的一个属性为可以select 才行。
TableView.allowsSelection=YES;
cell.selectionStyle=UITableViewCellSelectionStyleBlue;
//如果不希望响应select,那么就可以用下面的代码设置属性:
.allowsSelection=NO;
//下面是响应select 点击函数,根据哪个section,哪个row 自己做出响应就好啦。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 1)
{
return;
}
else if(indexPath.section==0)
{
switch (indexPath.row)
{
//聊天
case 0:
{
[self onTalkToFriendBtn];
}
break;
default:
break;
}
}
else
{
return ;
}
//常规的跳转到detail页:
PostDetail *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"PostDetail_tvc"];
//跳转到detail页也可以传值:
NSDictionary *dicPost = [listData objectAtIndex:indexPath.row];
detail.postDetail = dicPost;
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.navigationController pushViewController:detail animated:YES]; //这个方法还可以popViewController | setViewController
}
//如何让cell 能够响应 select,但是选中后的颜色又不发生改变呢,那么就设置
cell.selectionStyle = UITableViewCellSelectionStyleNone;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//cell被选中后的颜色不变
cell.selectionStyle = UITableViewCellSelectionStyleNone;
//在这个案例中,我们将backgroundView设置为显示一幅外边缘有一定透明度的图片,因此backgroundColor的显示将位于图片背后。我们将selectedBackgroundView设置为一个几乎透明的黑色长方形,当单元格被选中时图片将变黑。同时我们为textLabel提供整幅背景色,于是我们其他的内容透过背景色显示:
UIImageView* v = [[UIImageViewalloc] initWithFrame:cell.bounds];
v.contentMode = UIViewContentModeScaleToFill;
v.image = [UIImageimageNamed:@"linen.png"];
cell.backgroundView = v;
////(9)如何设置tableview 每行之间的 分割线
self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
//如果不需要分割线,那么就设置属性为 UITableViewCellSeparatorStyleNone 即可。
////(10)如何设置 tableview cell的背景颜色
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
//设置背景颜色
cell.contentView.backgroundColor=[UIColor colorWithRed:0.957 green:0.957 blue:0.957 alpha:1];
////(11)这个函数响应,用户点击cell 右边的 箭头(如果有的话)
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
////(12)如何设置tableview 可以被编辑
////首先要进入编辑模式:
[TableView setEditing:YES animated:YES];
////如果要退出编辑模式,肯定就是设置为NO
////返回当前cell 要执行的是哪种编辑,下面的代码是 返回 删除 模式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
-(void) tableView:(UITableView *)aTableView
commitEditingStyle:(UITableViewCellEditingStyle) editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
////通知告诉用户编辑了 哪个cell,对应上面的代码,我们在这个函数里面执行删除cell的操作。
-(void) tableView:(UITableView *)aTableView
commitEditingStyle:(UITableViewCellEditingStyle) editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
[chatArray removeObjectAtIndex:indexPath.row];
[chatTableView reloadData];
////(13)如何获得 某一行的CELL对象
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;
--EOF--
-(void) header:(NSString *) sharp {
NSRange cursorPosition = [markdownTextView selectedRange];
NSMutableString *tfContent = [[NSMutableString alloc] initWithString:[markdownTextView text]];
[tfContent insertString:sharp atIndex:cursorPosition.location];
[markdownTextView setText:tfContent];
}
- (void) replaceCharactersInRange:(NSRange)range withString:(NSString *)newText
{
markdownTextView.scrollEnabled = NO;
NSMutableString *textStorage = [markdownTextView.text mutableCopy];
[textStorage replaceCharactersInRange:range withString:newText];
//replace text but undo manager is not working well
[[self.undoManager prepareWithInvocationTarget:self] replaceCharactersInRange:NSMakeRange(range.location, newText.length)
withString:[textStorage substringWithRange:range]];
NSLog(@"before replacing: canUndo:%d", [self.undoManager canUndo]); //prints YES
markdownTextView.text = textStorage;
NSLog(@"after replacing: canUndo:%d", [self.undoManager canUndo]); //prints NO
if (![self.undoManager isUndoing])[self.undoManager setActionName:@"replace characters"];
//new range:
range.location = range.location + newText.length;
range.length = 0;
markdownTextView.selectedRange = range;
markdownTextView.scrollEnabled = YES;
}
//replace the selection with a word in UITextView
NSRange range = textView.selectedRange;
if (range.location != NSNotFound) {
NSString *replacement = @"some word";
textView.text = [textView.text stringByReplacingCharactersInRange:range
withString:replacement];
}
//***************
// 增加title
//***************
self.title = [NSStringstringWithFormat:@"与%@的私信",currentName];
//***************
//check if a view exists
//***************
UIView* barview = [cell viewWithTag:123221];
if (barview != nil) {
...
}
//Otherwise, you need to iterate through the .subviews array and check if the property matches, e.g.
UIView* barview = nil;
for (UIView* subview in cell.subviews) {
if ([subview isKindOfClass:[BarView class]]) {
barview = subview;
break;
}
}
if (barview != nil) {
...
}
@xxd
Copy link
Author

xxd commented Jun 26, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment