Objective-C UIButton&viewDidLoad

我是一个正在学习OC的新手 .

但是现在当我将一个插座按钮设置为我的viewcontroller的属性时,为了为它添加一个属性,当我在 viewDidLoad 中编写一些方法时,发生了类似这样的错误:

我唯一想做的就是设置 outLetButton.currentTitle 的属性

我的代码如下:

我的按钮没有问题 .

(void)viewDidLoad {

[super viewDidLoad];

/

NSMutableAttributedString * title = [[NSMutableString alloc] initWithString:self.OutletButton.currentTitle] ;

[title setAttributes:@{NSStrokeWidthAttributeName:@3.0,NSStrokeColorAttributeName:[self.OutletButton tintColor]} range:NSMakeRange(0, [title length])] ;

[self.OutletButton setAttributedTitle:title forState:UIControlStateNormal] ;

}

我使用突破性调试,当我跨过 [title setAttributes...] 时,它崩溃了 .

First throw call stack

0 CoreFoundation 0x000000010ba0b12b __exceptionPreprocess 171

1 libobjc.A.dylib 0x000000010b09ff41 objc_exception_throw 48

2 CoreFoundation 0x000000010ba8c024 - [NSObject(NSObject)doesNotRecognizeSelector:] 132

3 CoreFoundation 0x000000010b98df78 forwarding 1432

4 CoreFoundation 0x000000010b98d958 _CF_forwarding_prep_0 120

5 UITextView 0x000000010a791106 - [ViewController viewDidLoad] 598

6 UIKit 0x000000010c03146c - [UIViewController loadViewIfRequired] 1235

7 UIKit 0x000000010c0318b9 - [UIViewController视图] 27

8 UIKit 0x000000010befc7cf - [UIWindow addRootViewControllerViewIfPossible] 122

9 UIKit 0x000000010befced7 - [UIWindow _setHidden:forced:] 294

10 UIKit 0x000000010bf0fe54 - [UIWindow makeKeyAndVisible] 42

11 UIKit 0x000000010be828b8 - [UIApplication _callInitializationDelegatesForMainScene:transitionContext:] 4737

12 UIKit 0x000000010be87aeb - [UIApplication _runWithMainScene:transitionContext:completion:] 1720

13 UIKit 0x000000010c2516f8 __111 - [__ UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] _ block_invoke 924

14 UIKit 0x000000010c6274c8 [_UICanvas _enqueuePostSettingUpdateTransactionBlock:] 153

15 UIKit 0x000000010c2512f1 - [__ UICanvasLifecycleMonitor_Compatability _scheduleFirstCommitForScene:transition:firstActivation:completion:] 249

16 UIKit 0x000000010c251b6b - [__ UICanvasLifecycleMonitor_Compatability activateEventsOnly:withContext:completion:] 696

17 UIKit 0x000000010cbcfa69 __82 - [_ UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] _ block_invoke 262

18 UIKit 0x000000010cbcf922 - [_ UIApplicationCanvas _transitionLifecycleStateWithTransitionContext:completion:] 444

19 UIKit 0x000000010c8ac9c8 __125 - [_ UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] _ block_invoke 221

20 UIKit 0x000000010caabb06 _performActionsWithDelayForTransitionContext 100

21 UIKit 0x000000010c8ac88b - [_ UICanvasLifecycleSettingsDiffAction performActionsForCanvas:withUpdatedScene:settingsDiff:fromSettings:transitionContext:] 231

22 UIKit 0x000000010c626b25 - [_ UICanvas场景:didUpdateWithDiff:transitionContext:completion:] 392

23 UIKit 0x000000010be8636a - [UIApplication workspace:didCreateScene:withTransitionContext:completion:] 523

24 UIKit 0x000000010c461605 - [UIApplicationSceneClientAgent场景:didInitializeWithEvent:完成:] 369

25 FrontBoardServices 0x0000000110158cc0 - [FBSSceneImpl _didCreateWithTransitionContext:completion:] 338

26 FrontBoardServices 0x00000001101617b5 __56- [FBSWorkspace客户端:handleCreateScene:withCompletion:] _ block_invoke_2 235

27 libdispatch.dylib 0x000000010f3b733d _dispatch_client_callout 8

28 libdispatch.dylib 0x000000010f3bc9f3 _dispatch_block_invoke_direct 592

29 FrontBoardServices 0x000000011018d498 FBSSERIALQUEUE_IS_CALLING_OUT_TO_A_BLOCK 24

30 FrontBoardServices 0x000000011018d14e - [FBSSerialQueue _performNext] 464

31 FrontBoardServices 0x000000011018d6bd - [FBSSerialQueue _performNextFromRunLoopSource] 45

32 CoreFoundation 0x000000010b9ae101 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION 17

33 CoreFoundation 0x000000010ba4df71 __CFRunLoopDoSource0 81

34 CoreFoundation 0x000000010b992a19 __CFRunLoopDoSources0 185

35 CoreFoundation 0x000000010b991fff __CFRunLoopRun 1279

36 CoreFoundation 0x000000010b991889 CFRunLoopRunSpecific 409

37 GraphicsServices 0x0000000110a219c6 GSEventRunModal 62

38 UIKit 0x000000010be895d6 UIApplicationMain 159

39 UITextView 0x000000010a7914df main 111

40 libdyld.dylib 0x000000010f433d81 start 1

41 ??? 0x0000000000000001 0x0 1

libc abi.dylib:以NSException类型的未捕获异常终止

回答(1)

2 years ago

问题是因为您使用 NSMutableString 初始化 NSMutableAttributedString 然后更改其属性 .

代码允许您初始化变量但 NSMutableString 不允许您添加属性,因此它崩溃..

代码应该是这样的..(将分配类更改为 NSMutableAttributedString

NSMutableAttributedString * title = [[NSMutableAttributedString alloc] initWithString:self.OutletButton.currentTitle] ;

[title setAttributes:@{NSStrokeWidthAttributeName:@3.0,NSStrokeColorAttributeName:[self.OutletButton tintColor]} range:NSMakeRange(0, [title length])] ;

[self.OutletButton setAttributedTitle:title forState:UIControlStateNormal] ;

谢谢 .