0
votes

I am a newbie who is learning OC.

But now when I set an outlet button as a property of my viewcontroller, in order to add an attribute for it, when I write some methods in viewDidLoad, something wrong happened like this:

The only thing I want to do is to set the attributes of the outLetButton.currentTitle

And my code is as follows:

There is nothing wrong with my button.

(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] ;

}

I used breakout debugging, when I step over [title setAttributes...], it crashed.

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 view] + 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 scene:didUpdateWithDiff:transitionContext:completion:] + 392

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

24 UIKit 0x000000010c461605 -[UIApplicationSceneClientAgent scene:didInitializeWithEvent:completion:] + 369

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

26 FrontBoardServices 0x00000001101617b5 __56-[FBSWorkspace client: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: terminating with uncaught exception of type NSException

1

1 Answers

0
votes

The issue is because you are initialising NSMutableAttributedString with NSMutableString & then changes its attribute..

The code lets you initialise the variable But NSMutableString doesn't allow you to add attributes so it crashes..

The code should be like this.. (change allocation class to 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] ;

Thanks.