显示 UIBarButtonItem 中不同点击次数的不同视图(包装 UISegmentedControl)

发布于 2024-10-07 10:43:30 字数 3848 浏览 7 评论 0原文

我已经阅读过有关 UITouch 和 UIGestureRecognizer 的内容,但我仍然很困惑它们之间的区别。 我有一个案例。在我的应用程序中,我有一个栏按钮项,我想在点击该按钮时显示不同的视图。如果我单击一次,我想显示一个文本视图,但是当我再次单击一次时,我想从该按钮显示一个弹出窗口。有没有人可以给我一个示例代码来做到这一点,并稍微解释一下 UITouch 和 UIGestureRecognizer 之间的区别???

更新

barbuttonitem 是 UISegmentedControl 的包装,这是来自 desain 的图片 alt text

我尝试使用touchesBegan:withEvent: 和touchesEnded:withEvent: 来解决这个问题,但我不知道如何将其连接到该栏按钮项目。 这是我编写的代码:

-(void)addSegment{


NSAutoreleasePool *pool;
int count_doc = [_docsegmentmodels count];
NSLog(@"count doc add segment : %d", count_doc);
pool = [[NSAutoreleasePool alloc] init];
DocSegmentedModel *sl;
NSMutableArray *segmentTextMutable = [NSMutableArray array];

for(int i=0 ;(i<count_doc && i < max_segment);i++){
    sl = [_docsegmentmodels objectAtIndex:i];
    NSString *evalString = [[KoderAppDelegate sharedAppDelegate] setStringWithLength:sl.docSegmentFileName:10];  
    [segmentTextMutable addObject:NSLocalizedString(evalString,@"")];

}



NSArray *segmentText = [segmentTextMutable copy];

_docSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentText];
_docSegmentedControl.selectedSegmentIndex = 0; 
_docSegmentedControl.autoresizingMask =  UIViewAutoresizingFlexibleHeight;
_docSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;//UISegmentedControlStylePlain;// UISegmentedControlStyleBar;//UISegmentedControlStyleBezeled;
//docSegmentedControl.frame = CGRectMake(0, 0, 800, 46.0);
[_docSegmentedControl addTarget:self action:@selector(docSegmentAction:) forControlEvents:UIControlEventValueChanged];

// Add the control to the navigation bar
//UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:_docSegmentedControl];
segmentItem = [[UIBarButtonItem alloc] initWithCustomView:_docSegmentedControl];
self.navItem.leftBarButtonItem = segmentItem;
self.navItem.leftBarButtonItem.title = @"";


[pool release];
[segmentItem release];
[_docSegmentedControl release];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[NSObject cancelPreviousPerformRequestsWithTarget:self 
                                         selector:@selector(segmentItemTapped:) object:segmentItem];

}

-(void)touchesEnd:(NSSet *)touches withEvent:(UIEvent *)event{
if (touches.count == 1) {
    if (theTouch.tapCount == 2) {
        [self performSelector:@selector(segmentItemTapped:)withObject:segmentItem afterDelay:0.35];
    }else {
        [self performSelector:@selector(docSegmentAction:) withObject:segmentItem afterDelay:0.0];
    }
}   

}

- (IBAction)segmentItemTapped:(id)sender{  

if (self.fileProperties == nil) {
    self.fileProperties = [[[FilePropertiesViewController alloc] initWithNibName:@"FilePropertiesViewController" bundle:nil]autorelease];
    fileProperties.delegate = self;
    self.filePropertiesPopover = [[[UIPopoverController alloc] initWithContentViewController:fileProperties]autorelease];
    [_docsegmentmodels objectAtIndex:_docSegmentedControl.selectedSegmentIndex];
}

fileProperties.docProperties = _docsegmentmodels;
fileProperties.index = _docSegmentedControl.selectedSegmentIndex; //index utk nilai2 di textfield
[self.filePropertiesPopover presentPopoverFromBarButtonItem:sender 
                                   permittedArrowDirections:UIPopoverArrowDirectionUp
                                                   animated:YES];

}

- (IBAction)docSegmentAction:(id)sender{
NSLog(@"open file");
isFileOpen = YES;
[self.moreFilePopOverController dismissPopoverAnimated:YES];
[self.filePropertiesPopover dismissPopoverAnimated:YES];

[self showTextView];

}

我的理解有什么错误吗?

i have read about UITouch and UIGestureRecognizer, but i still really confused what the difference between them.
I have one case. In my app, i have a barbuttonitem, i want to show different view when a tap that button. if i do a singletap, i want to show a textview, but when i do a singletap again, i want to show a popover from that button. is there somebody can give me an example code to do it and give a little explanation about what is the difference between UITouch and UIGestureRecognizer???

UPDATE

the barbuttonitem is a wrapper of UISegmentedControl, here's a pic from the desain
alt text

i was try to use touchesBegan:withEvent: and touchesEnded:withEvent: to solve this problem, but i dont know how to connect it to that barbuttonitem.
This is the code i made :

-(void)addSegment{


NSAutoreleasePool *pool;
int count_doc = [_docsegmentmodels count];
NSLog(@"count doc add segment : %d", count_doc);
pool = [[NSAutoreleasePool alloc] init];
DocSegmentedModel *sl;
NSMutableArray *segmentTextMutable = [NSMutableArray array];

for(int i=0 ;(i<count_doc && i < max_segment);i++){
    sl = [_docsegmentmodels objectAtIndex:i];
    NSString *evalString = [[KoderAppDelegate sharedAppDelegate] setStringWithLength:sl.docSegmentFileName:10];  
    [segmentTextMutable addObject:NSLocalizedString(evalString,@"")];

}



NSArray *segmentText = [segmentTextMutable copy];

_docSegmentedControl = [[UISegmentedControl alloc] initWithItems:segmentText];
_docSegmentedControl.selectedSegmentIndex = 0; 
_docSegmentedControl.autoresizingMask =  UIViewAutoresizingFlexibleHeight;
_docSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBezeled;//UISegmentedControlStylePlain;// UISegmentedControlStyleBar;//UISegmentedControlStyleBezeled;
//docSegmentedControl.frame = CGRectMake(0, 0, 800, 46.0);
[_docSegmentedControl addTarget:self action:@selector(docSegmentAction:) forControlEvents:UIControlEventValueChanged];

// Add the control to the navigation bar
//UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:_docSegmentedControl];
segmentItem = [[UIBarButtonItem alloc] initWithCustomView:_docSegmentedControl];
self.navItem.leftBarButtonItem = segmentItem;
self.navItem.leftBarButtonItem.title = @"";


[pool release];
[segmentItem release];
[_docSegmentedControl release];

}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[NSObject cancelPreviousPerformRequestsWithTarget:self 
                                         selector:@selector(segmentItemTapped:) object:segmentItem];

}

-(void)touchesEnd:(NSSet *)touches withEvent:(UIEvent *)event{
if (touches.count == 1) {
    if (theTouch.tapCount == 2) {
        [self performSelector:@selector(segmentItemTapped:)withObject:segmentItem afterDelay:0.35];
    }else {
        [self performSelector:@selector(docSegmentAction:) withObject:segmentItem afterDelay:0.0];
    }
}   

}

- (IBAction)segmentItemTapped:(id)sender{  

if (self.fileProperties == nil) {
    self.fileProperties = [[[FilePropertiesViewController alloc] initWithNibName:@"FilePropertiesViewController" bundle:nil]autorelease];
    fileProperties.delegate = self;
    self.filePropertiesPopover = [[[UIPopoverController alloc] initWithContentViewController:fileProperties]autorelease];
    [_docsegmentmodels objectAtIndex:_docSegmentedControl.selectedSegmentIndex];
}

fileProperties.docProperties = _docsegmentmodels;
fileProperties.index = _docSegmentedControl.selectedSegmentIndex; //index utk nilai2 di textfield
[self.filePropertiesPopover presentPopoverFromBarButtonItem:sender 
                                   permittedArrowDirections:UIPopoverArrowDirectionUp
                                                   animated:YES];

}

- (IBAction)docSegmentAction:(id)sender{
NSLog(@"open file");
isFileOpen = YES;
[self.moreFilePopOverController dismissPopoverAnimated:YES];
[self.filePropertiesPopover dismissPopoverAnimated:YES];

[self showTextView];

}

is there any mistake in my understanding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶底世界 2024-10-14 10:43:30

在你的情况下,你不需要 UIGestureRecognizer。只需将 barbuttonitem 的操作设置为点击按钮时应调用的方法即可。让该方法跟踪文本视图的状态,并根据它显示它或显示弹出窗口。

In your case you don't need UIGestureRecognizer. Simply set the action of the barbuttonitem to the method that should be called when the button is tapped. Have the method keep track of the state of the textview and depending on it show it or show the popover.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文