在UITextField / UIWebView中缺少复制/粘贴菜单

时间:2011-06-20 16:36:39

标签: iphone ios ipad

我遇到了问题,我找不到解决方法。

我有一个UIWebView实施和UITextField的观点。 在两者上,当我点击时,我没有显示复制/粘贴菜单。

UIWebView中有文字(仅文字),我可以选择一个单词或一个段落,或者让放大镜出现并手动选择我想要的文字。

另一方面,UITextField可以接受输入并按预期工作,但复制/粘贴功能除外。

什么都没有分类。我只需要iOS的默认实现来复制/粘贴功能。

此问题不在单个视图中。我有另一个UIWebView在其他地方有同样的问题所以我认为这是一个全球问题。

我已经做了所有明显的事情(导入UIKit和Foundation框架,分配属性,释放等),但我再次坚持这一点。

什么可能会影响/干扰这样一个简单的功能,禁用它? 而且,总是在最简单的实现下,还需要做什么呢? (我遗失的任何框架,任何财产等)。

这么简单,我坚持下去。 如果有人有任何想法,它非常感激。

====编辑====

问题不是由我在任何视图或类中的代码引起的。

我添加了一个新视图(应用程序基于tabbar),只有一个UITextField和一个带有默认“Lorem Ipsum”文本的UITextView。 在textView上我也可以选择文本但没有菜单来复制/粘贴/选择/选择全部。 这也发生在textField(空)中,没有粘贴菜单出现(我从另一个应用程序复制一些文本,例如Safari或Notes)。

在所有观点中,问题似乎是普遍影响应用程序的其他地方。

我删除了Frameworks引用并将它们放回去但没有任何反应。 我还在试图弄清楚它的来源。

5 个答案:

答案 0 :(得分:7)

我遇到了同样的问题 - 其他一切都正常运行,但所有UITextView都缺少“复制/粘贴/选择”菜单,全局通过所有应用程序。

经过一些实验,我发现原因是:

“在启动时可见”属性未在MainWindow.xib中的窗口上设置

OR

在AppDelegate的[self.window makeKeyAndVisible]方法中缺少对application:didFinishLaunchingWithOptions:的调用。

在修复任何之后,它可以正常工作。试试吧。

答案 1 :(得分:3)

如果您使用Xcode 11+和iOS 13+项目(和SceneDelegate),

请确保您以正确的顺序应用更改:

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow()
        window!.rootViewController = rootNavigation
        window!.windowScene = windowScene
        window!.makeKeyAndVisible()
    }
}

因为,如果您将其应用如下:

    window!.makeKeyAndVisible()
    window!.windowScene = windowScene

您将描述有问题的问题。它会出现并且看起来很好,可以工作,但是不会出现复制粘贴操作。我花了一段时间才找到答案。

答案 2 :(得分:1)

确保您实施:

//init your menu somewhere, appropiately

- (id) initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {

        NSMutableArray *items = [[[UIMenuController sharedMenuController] menuItems] mutableCopy];
        if (!items) items = [[NSMutableArray alloc] init];

        UIMenuItem *menuItem;
        menuItem = [[UIMenuItem alloc] initWithTitle:@"Undo" action:@selector(undo:)];
        [items addObject:menuItem];
        [menuItem release];
        menuItem = [[UIMenuItem alloc] initWithTitle:@"Redo" action:@selector(redo:)];
        [items addObject:menuItem];
        [menuItem release];

        [[UIMenuController sharedMenuController] setMenuItems:items];
            [items release];

    }
    return self;
}

//allow other items to appear and yours too :) Perhaps you are missing this?
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if ([super canPerformAction:action withSender:sender]) {
        return YES;
    }
    else {
        //write your code here
        if (action == @selector(undo:) && [self.undoManager canUndo]) return YES;
        if (action == @selector(redo:) && [self.undoManager canRedo]) return YES;
    }
    return NO;
}

//Do your actions 
- (void)undo:(id)sender{
//do your stuff here
}
- (void)redo:(id)sender{
//do your stuff here
}

答案 3 :(得分:0)

好的,我找到了解决方案。 这与对象的代码或属性无关。 它或多或少是XCode4的“实现”。

当创建一个新的通用项目时,xcode会在项目的根目录下放置一个AppDelegate,在每个文件夹中放置另外两个AppDelegates,一个用于iPad,另一个用于iPhone。

根应用程序委托看起来像

#import <UIKit/UIKit.h> 
@interface SampleAppDelegate : NSObject <UIApplicationDelegate> { }
@property (nonatomic, retain) IBOutlet UIWindow *window;

#import "SampleAppDelegate.h"
@implementation SampleAppDelegate
@synthesize window=_window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    [self.window makeKeyAndVisible];
    return YES;
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
@end

iPhone和iPad应用程序代表没有“窗口”参考。

如果删除了根AppDelegate并在每个特定于设备的AppDelegate中移动了“窗口”代码,则会再次显示选择/选择全部/复制/粘贴(或任何其他选项)菜单。

在我创建的第一个新xcode4上发生了这种情况。我不记得xcode3是否有类似的实现(但我认为不是)。

答案 4 :(得分:0)

看起来,现在(2020年)iOS出现了错误。当您将UITextField的对齐方式更改为居中(NSTextAlignmentCenter)时,您将无法触发诸如为空字段粘贴之类的操作。

也许这也是您的问题。

相关问题