How to rename the IBOutlet property without lose the connection

前端 未结 4 2013

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, nonatomi         


        
相关标签:
4条回答
  • 2020-12-15 08:09

    I put my two cents in. I tried to refactor several similar IBOutlet-s and ended to complete successfully only one - the first one.

    All others outlets of this type throw me the error on refactoring:

    Project and XCode reloading doesn't solve the problem. I checked *.xib source - the first one is fine: My XCode ver 9.2(9C40b)

    0 讨论(0)
  • 2020-12-15 08:14

    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...".

     

    3. Change the outlet name

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

     

    The outlet is renamed in the code and storyboard

    @property (weak, nonatomic) IBOutlet UIButton *requestCodeButton;
    
    0 讨论(0)
  • 2020-12-15 08:16

    This is what I usually do

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

    1. Click find,then selected replace

    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

    0 讨论(0)
  • 2020-12-15 08:20

    If you are using Xcode Version 9.0 (release 9A235 or earlier beta) and you don't want to crash at UIApplicationMain() you will need to perform the following steps in order to rename an IBOutlet instance variable. (see below)

    Steps:

    1. Right-click the outlet instance variable
    2. Select "Refactor -> Rename..."
    3. Enter a new name Change all of them

    do

    1. Right-click the related '.xib' file (or files) in project file list
    2. Select "Open As -> Source Code" (it will show you the XML then...)
    3. Search and replace old variable name with new in .xib

    while ( untouched related .xib files exist)

    I wasted hours trying to figure out why the app was crashing in UIApplicationMain() after doing some mass refactoring of variable names. No logic changes, just hours of documenting, cleaning up structure for readability and just cleaning up names... not worth testing normally but when I decided to run.. BOOM

    As it turns out, the (ahem) "release" of Xcode 9 does not manage to actually refactor IBOutlet instance variable names so you relegated to hand-jamming the .xib files. True refactoring would make all the necessary changes in the project...

    Here is a regression tested example of how and what to do.

    Example:

    Select the IBOutlet instance variable, then right-click, select Refactor -> Rename... :

    (step 1 and 2)

    Now change the name. (for the example from textFieldController to editTextFieldController) :

    (step 3)

    Now for the critical part that Xcode neglects, change the custom outlet property in the XML:

    (steps 4, and 5)

    and then find the original variable (eg textFieldController) and change it to the new variable name (eg editTextFieldController)

    (step 6)

    ... and then when you compile and run, it doesn't throw an exception / assertion in UIApplicationMain() ...

    0 讨论(0)
提交回复
热议问题