ios - Objective-C - 因保留周期而丢失
<p><p>在解雇我的 VC 时,我注意到我没有从内存中释放所有内容。我对如何找到我的保留周期感到非常迷茫。我正在使用 NSTimer 和 NSNotificationCenter,但我确保在退出之前使和 removeObservers 无效,并且我确保使用弱委托(delegate)。 </p>
<p>我的保留周期还会发生在哪里?在动画 block 中?像这样?</p>
<pre><code> [UIView animateWithDuration:.1 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.setListTableViewVertConst.constant = 0;
self.setListTableViewHeightConst.constant = 264;
} completion:^(BOOL finished) {
}];
</code></pre>
<p>在使用 GCD 时,我确保使用 weakSelf。 </p>
<pre><code>__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
;
});
</code></pre>
<p>感谢您的帮助。 </p>
<p>编辑:</p>
<pre><code>-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//Send the room code to be displayed on the respective view controllers.
if () {
SetListRoomViewController *setListVC = segue.destinationViewController;
setListVC.roomCode = self.roomCodeTextField.text;
}
}
</code></pre>
<p>viewWIllApear</p>
<pre><code>;
self.socket = [socket];
self.socketID = [socketID];
NSString *roomCodeAsHost = [hostRoomCode];
/////////HOST/////////
if ([isHost]) {
[ addObserver:self
selector:@selector(receiveHostSongAddedNotification:)
name:kQueueAdd
object:nil];
[ addObserver:self
selector:@selector(receiveUserJoinedNotification:)
name:kUserJoined
object:nil];
NSLog(@"User is the host of this room");
self.isHost = YES;
;
self.roomCodeLabel.text = roomCodeAsHost;
if (!self.hostQueue) {
self.hostQueue = [init];
}
if (!self.hostCurrentArtist) {
self.hostCurrentArtist = [init];
}
if (!self.player) {
self.player = [init];
}
if (!self.timer) {
self.timer = [init];
}
}
///////NOT HOST///////
else {
// Add a notifcation observer and postNotification name for updating the tracks.
[ addObserver:self
selector:@selector(receiveQueueUpdatedNotification:)
name:kQueueUpdated
object:nil];
//Add a notifcation observer and postNotification name for updating current artist.
[ addObserver:self
selector:@selector(receiveCurrentArtistUpdateNotification:)
name:kCurrentArtistUpdate
object:nil];
[ addObserver:self
selector:@selector(receiveOnDisconnectNotification:)
name:kOnDisconnect
object:nil];
[ addObserver:self
selector:@selector(receiveHostDisconnectNotification:)
name:kHostDisconnect
object:nil];
//Add some animations upon load up. Purple glow and tableview animation.
double delay = .4;
;
withRowAnimation:UITableViewRowAnimationBottom];
//Set Current artist, if there is one.
NSDictionary *currentArtist = [currentArtist];
;
//Set the current tracks, if there is one.
NSArray *setListTracks = [setListTracks];
if (setListTracks) {
self.tracks = setListTracks;
}
}
}
</code></pre>
<p>tableVIEWs</p>
<pre><code>-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView.tag == 1) {
if (self.isHost) {
return ;
}
else return ;
}
else return ;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我注意到您在某些评论中使用了“popOut”一词,因此我假设您使用的是导航 Controller 。</p>
<p>如果是这种情况,您的 ViewController 将被嵌入它的导航 Controller 保留,并且不会被释放。导航 Controller 需要保存对您的 VC 的引用(请参阅 UINavigationController.viewControllers),以便在您弹出层次结构中的顶部/下一个 VC 时它可以返回到它。</p>
<p>这是预期的行为。</p></p>
<p style="font-size: 20px;">关于ios -Objective-C- 因保留周期而丢失,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/28270768/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/28270768/
</a>
</p>
页:
[1]