6

我正在尝试控制消息应用程序中的主题行。现在我只是想在主题字段中显示文本。

我的主要问题是让编译器将其识别_subjectLine为有效视图。这就是我尝试对/使用做任何事情时得到的结果_subjectLine

Tweak.xm:8: error: ‘_subjectLine’ was not declared in this scope

我不知道如何声明一个已经存在的项目以在调整中使用。我在 Xcode 中使用的标准声明(通常位于头文件中)似乎不一样。

我已经在谷歌上搜索了大约一个星期。我发现的最常见的教程或信息很简单:当方法激活时 - 显示警报。我能做到,没问题。但是,我需要使用一个已经存在的对象。

4

4 回答 4

10

It seems that in your case you are trying to use an instance variable of the class you are hooking. Modifying the instance variable does not work that way in tweaks. You have to use MSHookIvar to 'hook' an instance variable (aka ivar). Example:

[Tweak.xm/mm]

#import <substrate.h> // necessary
#import <Foundation/Foundation.h>

@interface TheClassYouAreHooking : NSObject {
    NSString *_exampleVariable;
}
- (void)doSomething;
@end

NSString *_exampleVariableHooked;

%hook TheClassYouAreHooking
- (void)doSomething 
{
    // 'Hook' the variable

    exampleVariableHooked = MSHookIvar<NSString *>(self, "_exampleVariable");

    // The name of the hooked variable does not need to be the same

    exampleVariableHooked = @"Hello World";

    // You can do ANYTHING with the object Eg. [exampleVariableHooked release];

}
%end

MSHookIvar can also hook stuff like BOOLs and floats etc.

exampleVariableHooked = MSHookIvar<BOOL>(self, "_someBOOL");

Its declared in substrate.h so you need to import that otherwise you will not be able to compile your tweak. Also as a bonus tip, I'm just reminding you that you have to put the identifier of the app/framework you're hooking in your tweakname.plist.

So after you 'hook' the variable you can change it to suit your needs. Happy coding!

于 2013-01-17T13:17:30.907 回答
2

您还可以使用 Objective-C 运行时函数来访问实例变量,如下所示:

UIView *subjectLine;
object_getInstanceVariable(self, "_subjectLine", (void **)&subjectLine);
于 2013-04-06T01:21:08.353 回答
1

您可以使用 KVC。示例:[object valueForKey:@"whatever"];

它可以在任何地方工作,并且比使用 Objective C 运行时方法或 Mobile Substrate 更干净。

于 2013-04-06T01:40:59.320 回答
1

我不熟悉 ChatKit,但快速浏览了一下。您无法访问 _subjectLine 因为它是 ivar。你应该只访问

id subject = [myCKContentEntryView subject]; // should return a CKTextContentView
NSAssert([subject isKindOfClass:[CKTextContentView class]], @"ack");
CKTextContentView * myTextContentView = subject;

CKTextContentView 有一个 setText 方法,但不知道它期望什么,因为参数是 id。可能是一个视图(UILabel?),也可能是一个字符串。你可以试试:

[myTextContentView setText:@"Hello World, w/ jimmies!"];

看看会发生什么。

于 2013-01-19T17:45:54.290 回答