Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename the IBOutlet property without lose the connection

Tags:

xcode

ios

I work with old project and in some part of the project, the naming convention is not good so I want to change it.
For example:

@property (weak, nonatomic) IBOutlet UIButton *btnRequestCode;
rename it to -> @property (weak, nonatomic) IBOutlet UIButton *requestCodeButton;

I know that I can delete the IBOutlet then reconnect but it may take many time and easy make error if careless

Any safe and fast way to do it?

I have tried Refactor -> Rename but it doesn't work and give me an warning

enter image description here

Any help or suggestion would be great appreciated.

like image 922
Linh Avatar asked May 19 '16 03:05

Linh


People also ask

How do I unlink IBOutlet?

Select the view on the storyboard and then click the Connections Inspector. Then you can click the little x to remove an outlet reference.

What is IBOutlet and IBAction?

@IBAction is similar to @IBOutlet , but goes the other way: @IBOutlet is a way of connecting code to storyboard layouts, and @IBAction is a way of making storyboard layouts trigger code. This method takes one parameter, called sender . It's of type UIButton because we know that's what will be calling the method.

What is Xcode IBOutlet?

The type qualifier IBOutlet is a tag applied to an property declaration so that the Interface Builder application can recognize the property as an outlet and synchronize the display and connection of it with Xcode. An outlet is declared as a weak reference ( weak ) to prevent strong reference cycles.


2 Answers

This is what I usually do

  1. Right Click the IBOutlet name,then selected Find Selected Text in Workspace

enter image description here

  1. Click find,then selected replace

enter image description here

3.Replace to the name you want

4.You can click Replace All if you are sure. You can also to click preview to Replace every single change

enter image description here

like image 118
Leo Avatar answered Sep 27 '22 20:09

Leo


Xcode 9, Refactor > Rename

New in Xcode 9 you can rename an outlet (or action) in the code and storyboard at the same time.

*DISCLAIMER: This feature only works in Xcode 9 (released September 2017) and above. Make sure to backup your project before using it. After making outlet name changes, test the affected screen(s) to make sure there are not issues. I tested this in a sample Objective C project successfully but it could cause problems in existing, more complex projects.

To rename an outlet...  

1. Navigate to outlet in the header file

@property (weak, nonatomic) IBOutlet UIButton *btnRequestCode;

 

2. Right click on outlet, select "Refactor > Rename...".

Right click on outlet

 

3. Change the outlet name

4. Select the "Rename" button or the Enter key

Change the outlet name

 

The outlet is renamed in the code and storyboard

@property (weak, nonatomic) IBOutlet UIButton *requestCodeButton;
like image 25
Mobile Dan Avatar answered Sep 27 '22 19:09

Mobile Dan