You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Objective-C与UIKit项目中使用并配置TipKit?

在Objective-C/UIKit中使用TipKit

一、TipKit初始化配置

你提到的Swift中Tips.configure()对应的Objective-C实现,无需修改main函数,建议在App启动的生命周期方法中完成异步配置:

1. 基于AppDelegate架构的情况

在application:didFinishLaunchingWithOptions:中调用配置方法:

#import <UIKit/UIKit.h>
#import <TipKit/TipKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // 初始化TipKit
    [Tips configureWithCompletionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"TipKit配置失败: %@", error.localizedDescription);
        }
    }];
    
    // 其他启动逻辑
    return YES;
}

@end

2. 基于SceneDelegate架构的情况

在scene:willConnectToSession:options:中调用配置方法:

#import <TipKit/TipKit.h>

@implementation SceneDelegate

- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
    [Tips configureWithCompletionHandler:^(NSError * _Nullable error) {
        if (error) {
            NSLog(@"TipKit配置失败: %@", error.localizedDescription);
        }
    }];
    
    // 其他场景初始化逻辑
}

@end

二、在UIKit中展示Tip

由于TipKit的核心Tip模型仅支持Swift定义,需要先通过Swift创建Tip,再通过桥接暴露给Objective-C,配合TipUIPopoverViewController完成展示:

1. 定义Swift Tip模型

创建CustomTips.swift文件:

import TipKit

@objcMembers
class CustomTips: NSObject {
    static let editFeatureTip = Tip(
        title: "新编辑功能",
        message: "点击此按钮快速编辑内容",
        image: UIImage(systemName: "pencil.circle")
    )
}

2. 配置桥接文件

在项目的桥接头文件(如YourProject-Bridging-Header.h)中添加:

#import <TipKit/TipKit.h>
@class CustomTips;

3. 在Objective-C中展示Tip

在需要展示Tip的ViewController中,调用以下代码:

#import "YourProject-Bridging-Header.h"

- (void)showEditFeatureTip {
    // 获取Swift定义的Tip实例
    Tip *tip = CustomTips.editFeatureTip;
    
    // 检查是否符合展示条件
    [tip shouldDisplayWithCompletionHandler:^(BOOL shouldDisplay) {
        if (!shouldDisplay) return;
        
        // 创建Tip弹出视图控制器
        TipUIPopoverViewController *tipVC = [[TipUIPopoverViewController alloc] initWithTip:tip];
        
        // 设置弹出锚点(示例为某个按钮)
        tipVC.sourceView = self.editButton;
        tipVC.sourceRect = self.editButton.bounds;
        tipVC.permittedArrowDirections = UIPopoverArrowDirectionUp;
        
        // 以Popover方式展示
        tipVC.modalPresentationStyle = UIModalPresentationPopover;
        [self presentViewController:tipVC animated:YES completion:nil];
    }];
}

内容的提问来源于stack exchange,提问作者Braeden Meikle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 18:56:11